Skip to content

Commit 3b5dd63

Browse files
dstrain115rht
authored andcommitted
Allow initialization of MeasurementKeys with empty strings (quantumlib#4445)
- An empty string is still a string and is used occasionally as a placeholder in user code. We should support creation of measurement keys with empty strings.
1 parent 5ee7b6c commit 3b5dd63

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

cirq-core/cirq/ops/measurement_gate_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def test_measure_init(num_qubits):
5252
cirq.MeasurementGate(5, 'a', invert_mask=(True,) * 6)
5353
with pytest.raises(ValueError, match='len.* !='):
5454
cirq.MeasurementGate(5, 'a', qid_shape=(1, 2))
55-
with pytest.raises(ValueError, match='cannot be empty'):
56-
cirq.MeasurementGate(2, qid_shape=(1, 2))
55+
with pytest.raises(ValueError, match='valid string'):
56+
cirq.MeasurementGate(2, qid_shape=(1, 2), key=None)
5757
with pytest.raises(ValueError, match='Specify either'):
5858
cirq.MeasurementGate()
5959

cirq-core/cirq/value/measurement_key.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class MeasurementKey:
4141
path: Tuple[str, ...] = dataclasses.field(default_factory=tuple)
4242

4343
def __post_init__(self):
44-
if not self.name:
45-
raise ValueError("Measurement key name cannot be empty")
44+
if not isinstance(self.name, str):
45+
raise ValueError("Measurement key name must be a valid string.")
4646
if MEASUREMENT_KEY_SEPARATOR in self.name:
4747
raise ValueError(
4848
f'Invalid key name: {self.name}\n{MEASUREMENT_KEY_SEPARATOR} is not allowed in '

cirq-core/cirq/value/measurement_key_test.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
def test_empty_init():
2121
with pytest.raises(TypeError, match='required positional argument'):
2222
_ = cirq.MeasurementKey()
23-
with pytest.raises(ValueError, match='cannot be empty'):
24-
_ = cirq.MeasurementKey('')
23+
with pytest.raises(ValueError, match='valid string'):
24+
_ = cirq.MeasurementKey(None)
25+
with pytest.raises(ValueError, match='valid string'):
26+
_ = cirq.MeasurementKey(4.2)
27+
# Initialization of empty string should be allowed
28+
_ = cirq.MeasurementKey('')
2529

2630

2731
def test_nested_key():

0 commit comments

Comments
 (0)