Skip to content

Commit db183d8

Browse files
pavoljuhasBichengYing
authored andcommitted
Fix flakes in pauli_string_measurement_with_readout_mitigation_test (quantumlib#7185)
* Deflake pauli_string_measurement_with_readout_mitigation_test Ensure random Pauli strings have a non-identity operator for real. * Add missing spaces to exception message * Simplify - make do without an extra local variable
1 parent 04167b3 commit db183d8

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

cirq-core/cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def _validate_input(
8383

8484
if all(q == ops.I for q in pauli_str):
8585
raise ValueError(
86-
"Empty Pauli strings or Pauli strings consisting"
87-
"only of Pauli I are not allowed. Please provide"
86+
"Empty Pauli strings or Pauli strings consisting "
87+
"only of Pauli I are not allowed. Please provide "
8888
"valid input Pauli strings."
8989
)
9090
if pauli_str.coefficient.imag != 0:

cirq-core/cirq/contrib/paulistring/pauli_string_measurement_with_readout_mitigation_test.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ def _create_ghz(number_of_qubits: int, qubits: Sequence[cirq.Qid]) -> cirq.Circu
3636
def _generate_random_pauli_string(qubits: Sequence[cirq.Qid], enable_coeff: bool = False):
3737
pauli_ops = [cirq.I, cirq.X, cirq.Y, cirq.Z]
3838

39-
# Ensure at least one non-identity.
40-
operators = {q: cirq.I(q) for q in qubits} # Start with all identities
41-
# Choose a random subset of qubits to have non-identity operators
42-
non_identity_qubits = random.sample(qubits, random.randint(1, len(qubits)))
43-
for q in non_identity_qubits:
44-
operators[q] = random.choice([cirq.X, cirq.Y, cirq.Z])(q) # Only non-identity ops
4539
operators = {q: random.choice(pauli_ops) for q in qubits}
40+
# Ensure at least one non-identity.
41+
operators[random.choice(qubits)] = random.choice(pauli_ops[1:])
4642

4743
if enable_coeff:
4844
coefficient = (2 * random.random() - 1) * 100
@@ -461,8 +457,8 @@ def test_all_pauli_strings_are_pauli_i() -> None:
461457

462458
with pytest.raises(
463459
ValueError,
464-
match="Empty Pauli strings or Pauli strings consisting"
465-
"only of Pauli I are not allowed. Please provide"
460+
match="Empty Pauli strings or Pauli strings consisting "
461+
"only of Pauli I are not allowed. Please provide "
466462
"valid input Pauli strings.",
467463
):
468464
measure_pauli_strings(

0 commit comments

Comments
 (0)