Skip to content

Commit

Permalink
Fix error and add test for mutable pauli string (quantumlib#5213)
Browse files Browse the repository at this point in the history
quantumlib#4001 looks abandoned so this does the change and adds the test as requested.
  • Loading branch information
dabacon authored and tonybruguier committed Apr 19, 2022
1 parent 203035a commit 31e521c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/pauli_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,6 @@ def _pauli_like_to_pauli_int(key: Any, pauli_gate_like: PAULI_GATE_LIKE):
f'Expected {key!r}: {pauli_gate_like!r} to have a '
f'cirq.PAULI_GATE_LIKE value. '
f"But the value isn't in "
f"{list(PAULI_GATE_LIKE_TO_INDEX_MAP.values())!r}"
f"{set(PAULI_GATE_LIKE_TO_INDEX_MAP.keys())!r}"
)
return cast(int, pauli_int)
16 changes: 16 additions & 0 deletions cirq-core/cirq/ops/pauli_string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,22 @@ def test_mutable_pauli_string_dict_functionality():
assert b not in p


@pytest.mark.parametrize(
'pauli', (cirq.X, cirq.Y, cirq.Z, cirq.I, "I", "X", "Y", "Z", "i", "x", "y", "z", 0, 1, 2, 3)
)
def test_mutable_pauli_string_dict_pauli_like(pauli):
p = cirq.MutablePauliString()
# Check that is successfully converts.
p[0] = pauli


def test_mutable_pauli_string_dict_pauli_like_not_pauli_like():
p = cirq.MutablePauliString()
# Check error string includes terms like "X" in error message.
with pytest.raises(TypeError, match="PAULI_GATE_LIKE.*X"):
p[0] = 1.2


def test_mutable_pauli_string_text():
p = cirq.MutablePauliString(cirq.X(cirq.LineQubit(0)) * cirq.Y(cirq.LineQubit(1)))
assert str(cirq.MutablePauliString()) == "mutable I"
Expand Down

0 comments on commit 31e521c

Please sign in to comment.