From 98cccaf7113c72da3ca63f920e5933df3c0b5e6a Mon Sep 17 00:00:00 2001 From: Casey Duckering Date: Tue, 17 Jul 2018 19:50:37 -0700 Subject: [PATCH 1/2] Move PauliString to ops --- cirq/{contrib/paulistring => ops}/clifford_gate.py | 0 cirq/{contrib/paulistring => ops}/clifford_gate_test.py | 0 cirq/{contrib/paulistring => ops}/pauli.py | 0 cirq/{contrib/paulistring => ops}/pauli_string.py | 0 cirq/{contrib/paulistring => ops}/pauli_string_test.py | 0 cirq/{contrib/paulistring => ops}/pauli_test.py | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename cirq/{contrib/paulistring => ops}/clifford_gate.py (100%) rename cirq/{contrib/paulistring => ops}/clifford_gate_test.py (100%) rename cirq/{contrib/paulistring => ops}/pauli.py (100%) rename cirq/{contrib/paulistring => ops}/pauli_string.py (100%) rename cirq/{contrib/paulistring => ops}/pauli_string_test.py (100%) rename cirq/{contrib/paulistring => ops}/pauli_test.py (100%) diff --git a/cirq/contrib/paulistring/clifford_gate.py b/cirq/ops/clifford_gate.py similarity index 100% rename from cirq/contrib/paulistring/clifford_gate.py rename to cirq/ops/clifford_gate.py diff --git a/cirq/contrib/paulistring/clifford_gate_test.py b/cirq/ops/clifford_gate_test.py similarity index 100% rename from cirq/contrib/paulistring/clifford_gate_test.py rename to cirq/ops/clifford_gate_test.py diff --git a/cirq/contrib/paulistring/pauli.py b/cirq/ops/pauli.py similarity index 100% rename from cirq/contrib/paulistring/pauli.py rename to cirq/ops/pauli.py diff --git a/cirq/contrib/paulistring/pauli_string.py b/cirq/ops/pauli_string.py similarity index 100% rename from cirq/contrib/paulistring/pauli_string.py rename to cirq/ops/pauli_string.py diff --git a/cirq/contrib/paulistring/pauli_string_test.py b/cirq/ops/pauli_string_test.py similarity index 100% rename from cirq/contrib/paulistring/pauli_string_test.py rename to cirq/ops/pauli_string_test.py diff --git a/cirq/contrib/paulistring/pauli_test.py b/cirq/ops/pauli_test.py similarity index 100% rename from cirq/contrib/paulistring/pauli_test.py rename to cirq/ops/pauli_test.py From ad0bfc577c539f07888a04b167494a69f6ea2d64 Mon Sep 17 00:00:00 2001 From: Casey Duckering Date: Wed, 18 Jul 2018 17:15:44 -0700 Subject: [PATCH 2/2] Move PauliString to ops (2) --- cirq/__init__.py | 5 + cirq/contrib/paulistring/__init__.py | 13 - .../paulistring/pauli_string_phasor.py | 11 +- .../paulistring/pauli_string_phasor_test.py | 183 ++++++------ .../paulistring/pauli_string_raw_types.py | 2 +- .../pauli_string_raw_types_test.py | 14 +- cirq/ops/__init__.py | 13 + cirq/ops/clifford_gate.py | 30 +- cirq/ops/clifford_gate_test.py | 209 +++++++------- .../pauli_interaction_gate.py | 27 +- .../pauli_interaction_gate_test.py | 72 ++--- cirq/ops/pauli_string.py | 51 ++-- cirq/ops/pauli_string_test.py | 264 +++++++++--------- cirq/ops/pauli_test.py | 186 ++++++------ 14 files changed, 552 insertions(+), 528 deletions(-) rename cirq/{contrib/paulistring => ops}/pauli_interaction_gate.py (85%) rename cirq/{contrib/paulistring => ops}/pauli_interaction_gate_test.py (62%) diff --git a/cirq/__init__.py b/cirq/__init__.py index 4294da0c211..a65d7ed474f 100644 --- a/cirq/__init__.py +++ b/cirq/__init__.py @@ -127,6 +127,11 @@ ReversibleCompositeGate, ReversibleEffect, ParameterizableEffect, + Pauli, + CliffordGate, + PauliTransform, + PauliInteractionGate, + PauliString, PhaseableEffect, QasmConvertableGate, QasmConvertableOperation, diff --git a/cirq/contrib/paulistring/__init__.py b/cirq/contrib/paulistring/__init__.py index bb40b20ce07..5a3fc6e90da 100644 --- a/cirq/contrib/paulistring/__init__.py +++ b/cirq/contrib/paulistring/__init__.py @@ -12,19 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from cirq.contrib.paulistring.pauli import ( - Pauli, -) -from cirq.contrib.paulistring.clifford_gate import ( - CliffordGate, - PauliTransform, -) -from cirq.contrib.paulistring.pauli_interaction_gate import ( - PauliInteractionGate, -) -from cirq.contrib.paulistring.pauli_string import ( - PauliString, -) from cirq.contrib.paulistring.pauli_string_raw_types import ( PauliStringGateOperation, ) diff --git a/cirq/contrib/paulistring/pauli_string_phasor.py b/cirq/contrib/paulistring/pauli_string_phasor.py index b522cc9aff3..1af11843ee7 100644 --- a/cirq/contrib/paulistring/pauli_string_phasor.py +++ b/cirq/contrib/paulistring/pauli_string_phasor.py @@ -18,12 +18,11 @@ from cirq import ops, value, study, extension -from cirq.contrib.paulistring import ( - Pauli, - CliffordGate, - PauliString, - PauliStringGateOperation, -) +from cirq.ops.pauli import Pauli +from cirq.ops.clifford_gate import CliffordGate +from cirq.ops.pauli_string import PauliString +from cirq.contrib.paulistring.pauli_string_raw_types import ( + PauliStringGateOperation) T_DESIRED = TypeVar('T_DESIRED') diff --git a/cirq/contrib/paulistring/pauli_string_phasor_test.py b/cirq/contrib/paulistring/pauli_string_phasor_test.py index 5185eb3feec..d2d605f2052 100644 --- a/cirq/contrib/paulistring/pauli_string_phasor_test.py +++ b/cirq/contrib/paulistring/pauli_string_phasor_test.py @@ -17,12 +17,7 @@ import numpy as np import cirq - -from cirq.contrib.paulistring import ( - Pauli, - PauliString, - PauliStringPhasor, -) +from cirq.contrib.paulistring import PauliStringPhasor def _make_qubits(n): @@ -32,14 +27,17 @@ def _make_qubits(n): def test_eq_ne_hash(): q0, q1, q2 = _make_qubits(3) eq = cirq.testing.EqualsTester() - ps1 = PauliString({q0: Pauli.X, q1: Pauli.Y, q2: Pauli.Z}) - ps2 = PauliString({q0: Pauli.X, q1: Pauli.Y, q2: Pauli.X}) + ps1 = cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y, + q2: cirq.Pauli.Z}) + ps2 = cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y, + q2: cirq.Pauli.X}) eq.make_equality_group( - lambda: PauliStringPhasor(PauliString({}), half_turns=0.5), - lambda: PauliStringPhasor(PauliString({}), half_turns=-1.5), - lambda: PauliStringPhasor(PauliString({}), half_turns=2.5)) + lambda: PauliStringPhasor(cirq.PauliString({}), half_turns=0.5), + lambda: PauliStringPhasor(cirq.PauliString({}), half_turns=-1.5), + lambda: PauliStringPhasor(cirq.PauliString({}), half_turns=2.5)) eq.make_equality_group( - lambda: PauliStringPhasor(PauliString({}, True), half_turns=-0.5)) + lambda: PauliStringPhasor(cirq.PauliString({}, True), + half_turns=-0.5)) eq.add_equality_group( PauliStringPhasor(ps1), PauliStringPhasor(ps1, half_turns=1)) @@ -61,17 +59,19 @@ def test_eq_ne_hash(): def test_map_qubits(): q0, q1, q2, q3 = _make_qubits(4) qubit_map = {q1: q2, q0: q3} - before = PauliStringPhasor(PauliString({q0: Pauli.Z, q1: Pauli.Y}), - half_turns=0.1) - after = PauliStringPhasor(PauliString({q3: Pauli.Z, q2: Pauli.Y}), - half_turns=0.1) + before = PauliStringPhasor( + cirq.PauliString({q0: cirq.Pauli.Z, q1: cirq.Pauli.Y}), + half_turns=0.1) + after = PauliStringPhasor( + cirq.PauliString({q3: cirq.Pauli.Z, q2: cirq.Pauli.Y}), + half_turns=0.1) assert before.map_qubits(qubit_map) == after def test_extrapolate_effect(): - op1 = PauliStringPhasor(PauliString({}), half_turns=0.5) - op2 = PauliStringPhasor(PauliString({}), half_turns=1.5) - op3 = PauliStringPhasor(PauliString({}), half_turns=0.125) + op1 = PauliStringPhasor(cirq.PauliString({}), half_turns=0.5) + op2 = PauliStringPhasor(cirq.PauliString({}), half_turns=1.5) + op3 = PauliStringPhasor(cirq.PauliString({}), half_turns=0.125) assert op1 ** 3 == op2 assert op1 ** 0.25 == op3 @@ -79,30 +79,33 @@ def test_extrapolate_effect(): def test_extrapolate_effect_with_symbol(): eq = cirq.testing.EqualsTester() eq.add_equality_group( - PauliStringPhasor(PauliString({}), half_turns=cirq.value.Symbol('a')), - PauliStringPhasor(PauliString({})) ** cirq.value.Symbol('a')) + PauliStringPhasor(cirq.PauliString({}), + half_turns=cirq.value.Symbol('a')), + PauliStringPhasor(cirq.PauliString({})) ** cirq.value.Symbol('a')) eq.add_equality_group( - PauliStringPhasor(PauliString({})) ** cirq.value.Symbol('b')) + PauliStringPhasor(cirq.PauliString({})) ** cirq.value.Symbol('b')) with pytest.raises(ValueError): - _ = PauliStringPhasor(PauliString({}), half_turns=0.5 + _ = PauliStringPhasor(cirq.PauliString({}), half_turns=0.5 ) ** cirq.value.Symbol('b') with pytest.raises(ValueError): - _ = PauliStringPhasor(PauliString({}), half_turns=cirq.value.Symbol('a') + _ = PauliStringPhasor(cirq.PauliString({}), + half_turns=cirq.value.Symbol('a') ) ** 0.5 with pytest.raises(ValueError): - _ = PauliStringPhasor(PauliString({}), half_turns=cirq.value.Symbol('a') + _ = PauliStringPhasor(cirq.PauliString({}), + half_turns=cirq.value.Symbol('a') ) ** cirq.value.Symbol('b') def test_inverse(): - op1 = PauliStringPhasor(PauliString({}), half_turns=0.25) - op2 = PauliStringPhasor(PauliString({}), half_turns=-0.25) + op1 = PauliStringPhasor(cirq.PauliString({}), half_turns=0.25) + op2 = PauliStringPhasor(cirq.PauliString({}), half_turns=-0.25) assert op1.inverse() == op2 def test_try_cast_to(): class Dummy: pass - op = PauliStringPhasor(PauliString({})) + op = PauliStringPhasor(cirq.PauliString({})) ext = cirq.Extensions() assert not op.try_cast_to(cirq.CompositeOperation, ext) is None assert not op.try_cast_to(cirq.BoundedEffect, ext) is None @@ -111,7 +114,8 @@ class Dummy: pass assert not op.try_cast_to(cirq.ReversibleEffect, ext) is None assert op.try_cast_to(Dummy, ext) is None - op = PauliStringPhasor(PauliString({}), half_turns=cirq.value.Symbol('a')) + op = PauliStringPhasor(cirq.PauliString({}), + half_turns=cirq.value.Symbol('a')) ext = cirq.Extensions() assert not op.try_cast_to(cirq.CompositeOperation, ext) is None assert not op.try_cast_to(cirq.BoundedEffect, ext) is None @@ -123,17 +127,18 @@ class Dummy: pass def test_is_parametrized(): - op = PauliStringPhasor(PauliString({})) + op = PauliStringPhasor(cirq.PauliString({})) assert not op.is_parameterized() assert not (op ** 0.1).is_parameterized() assert (op ** cirq.value.Symbol('a')).is_parameterized() def test_with_parameters_resolved_by(): - op = PauliStringPhasor(PauliString({}), half_turns=cirq.value.Symbol('a')) + op = PauliStringPhasor(cirq.PauliString({}), + half_turns=cirq.value.Symbol('a')) resolver = cirq.study.ParamResolver({'a': 0.1}) actual = op.with_parameters_resolved_by(resolver) - expected = PauliStringPhasor(PauliString({}), half_turns=0.1) + expected = PauliStringPhasor(cirq.PauliString({}), half_turns=0.1) assert actual == expected @@ -141,14 +146,14 @@ def test_drop_negligible(): q0, = _make_qubits(1) sym = cirq.value.Symbol('a') circuit = cirq.Circuit.from_ops( - PauliStringPhasor(PauliString({q0: Pauli.Z})) ** 0.25, - PauliStringPhasor(PauliString({q0: Pauli.Z})) ** 1e-10, - PauliStringPhasor(PauliString({q0: Pauli.Z})) ** sym, - ) + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z})) ** 0.25, + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z})) ** 1e-10, + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z})) ** sym, + ) expected = cirq.Circuit.from_ops( - PauliStringPhasor(PauliString({q0: Pauli.Z})) ** 0.25, - PauliStringPhasor(PauliString({q0: Pauli.Z})) ** sym, - ) + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z})) ** 0.25, + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z})) ** sym, + ) cirq.DropNegligible().optimize_circuit(circuit) cirq.DropEmptyMoments().optimize_circuit(circuit) assert circuit == expected @@ -158,34 +163,34 @@ def test_manual_default_decompose(): q0, q1, q2 = _make_qubits(3) mat = cirq.Circuit.from_ops( - PauliStringPhasor(PauliString({q0: Pauli.Z})) ** 0.25, - cirq.Z(q0) ** -0.25, - ).to_unitary_matrix() + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z})) ** 0.25, + cirq.Z(q0) ** -0.25, + ).to_unitary_matrix() cirq.testing.assert_allclose_up_to_global_phase(mat, np.eye(2), rtol=1e-7, atol=1e-7) mat = cirq.Circuit.from_ops( - PauliStringPhasor(PauliString({q0: Pauli.Y})) ** 0.25, - cirq.Y(q0) ** -0.25, - ).to_unitary_matrix() + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Y})) ** 0.25, + cirq.Y(q0) ** -0.25, + ).to_unitary_matrix() cirq.testing.assert_allclose_up_to_global_phase(mat, np.eye(2), rtol=1e-7, atol=1e-7) mat = cirq.Circuit.from_ops( - PauliStringPhasor(PauliString({q0: Pauli.Z, - q1: Pauli.Z, - q2: Pauli.Z})) - ).to_unitary_matrix() + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z, + q1: cirq.Pauli.Z, + q2: cirq.Pauli.Z})) + ).to_unitary_matrix() cirq.testing.assert_allclose_up_to_global_phase( mat, np.diag([1, -1, -1, 1, -1, 1, 1, -1]), rtol=1e-7, atol=1e-7) mat = cirq.Circuit.from_ops( - PauliStringPhasor(PauliString({q0: Pauli.Z, - q1: Pauli.Y, - q2: Pauli.X})) ** 0.5 - ).to_unitary_matrix() + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z, + q1: cirq.Pauli.Y, + q2: cirq.Pauli.X})) ** 0.5 + ).to_unitary_matrix() cirq.testing.assert_allclose_up_to_global_phase( mat, np.array([ @@ -203,7 +208,7 @@ def test_manual_default_decompose(): @pytest.mark.parametrize('paulis,half_turns,neg', itertools.product( - itertools.product(Pauli.XYZ + (None,), repeat=3), + itertools.product(cirq.Pauli.XYZ + (None,), repeat=3), (0, 0.1, 0.5, 1, -0.25), (False, True))) def test_default_decompose(paulis, half_turns, neg): @@ -211,15 +216,15 @@ def test_default_decompose(paulis, half_turns, neg): qubits = _make_qubits(len(paulis)) # Get matrix from decomposition - pauli_string = PauliString({q: p for q, p in zip(qubits, paulis)}, neg) + pauli_string = cirq.PauliString({q: p for q, p in zip(qubits, paulis)}, neg) actual = cirq.Circuit.from_ops( - PauliStringPhasor(pauli_string, half_turns=half_turns) - ).to_unitary_matrix() + PauliStringPhasor(pauli_string, half_turns=half_turns) + ).to_unitary_matrix() # Calculate expected matrix - to_z_mats = {Pauli.X: (cirq.Y ** -0.5).matrix(), - Pauli.Y: (cirq.X ** 0.5).matrix(), - Pauli.Z: np.eye(2)} + to_z_mats = {cirq.Pauli.X: (cirq.Y ** -0.5).matrix(), + cirq.Pauli.Y: (cirq.X ** 0.5).matrix(), + cirq.Pauli.Z: np.eye(2)} expected_convert = np.eye(1) for pauli in paulis: expected_convert = np.kron(expected_convert, to_z_mats[pauli]) @@ -233,13 +238,13 @@ def test_default_decompose(paulis, half_turns, neg): def test_decompose_with_symbol(): q0, = _make_qubits(1) - ps = PauliString({q0: Pauli.Y}) + ps = cirq.PauliString({q0: cirq.Pauli.Y}) op = PauliStringPhasor(ps, half_turns=cirq.value.Symbol('a')) circuit = cirq.Circuit.from_ops(op) cirq.ExpandComposite().optimize_circuit(circuit) assert circuit.to_text_diagram() == "q0: ───X^0.5───Z^a───X^-0.5───" - ps = PauliString({q0: Pauli.Y}, True) + ps = cirq.PauliString({q0: cirq.Pauli.Y}, True) op = PauliStringPhasor(ps, half_turns=cirq.value.Symbol('a')) circuit = cirq.Circuit.from_ops(op) cirq.ExpandComposite().optimize_circuit(circuit) @@ -249,21 +254,21 @@ def test_decompose_with_symbol(): def test_text_diagram(): q0, q1, q2 = _make_qubits(3) circuit = cirq.Circuit.from_ops( - PauliStringPhasor(PauliString({q0: Pauli.Z})), - PauliStringPhasor(PauliString({q0: Pauli.Y})) ** 0.25, - PauliStringPhasor(PauliString({q0: Pauli.Z, - q1: Pauli.Z, - q2: Pauli.Z})), - PauliStringPhasor(PauliString({q0: Pauli.Z, - q1: Pauli.Y, - q2: Pauli.X}, True)) ** 0.5, - PauliStringPhasor(PauliString({q0: Pauli.Z, - q1: Pauli.Y, - q2: Pauli.X}), + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z})), + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Y})) ** 0.25, + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z, + q1: cirq.Pauli.Z, + q2: cirq.Pauli.Z})), + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z, + q1: cirq.Pauli.Y, + q2: cirq.Pauli.X}, True)) ** 0.5, + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z, + q1: cirq.Pauli.Y, + q2: cirq.Pauli.X}), half_turns=cirq.value.Symbol('a')), - PauliStringPhasor(PauliString({q0: Pauli.Z, - q1: Pauli.Y, - q2: Pauli.X}, True), + PauliStringPhasor(cirq.PauliString({q0: cirq.Pauli.Z, + q1: cirq.Pauli.Y, + q2: cirq.Pauli.X}, True), half_turns=cirq.value.Symbol('b'))) assert circuit.to_text_diagram() == """ q0: ───[Z]───[Y]^0.25───[Z]───[Z]────────[Z]─────[Z]────── @@ -276,27 +281,29 @@ def test_text_diagram(): def test_repr(): q0, q1, q2 = _make_qubits(3) - ps = PauliStringPhasor(PauliString({q2: Pauli.Z, - q1: Pauli.Y, - q0: Pauli.X})) ** 0.5 + ps = PauliStringPhasor(cirq.PauliString({q2: cirq.Pauli.Z, + q1: cirq.Pauli.Y, + q0: cirq.Pauli.X})) ** 0.5 assert (repr(ps) == 'PauliStringPhasor({+, q0:X, q1:Y, q2:Z}, half_turns=0.5)') - ps = PauliStringPhasor(PauliString({q2: Pauli.Z, - q1: Pauli.Y, - q0: Pauli.X}, True)) ** -0.5 + ps = PauliStringPhasor(cirq.PauliString({q2: cirq.Pauli.Z, + q1: cirq.Pauli.Y, + q0: cirq.Pauli.X}, True) + ) ** -0.5 assert (repr(ps) == 'PauliStringPhasor({-, q0:X, q1:Y, q2:Z}, half_turns=-0.5)') def test_str(): q0, q1, q2 = _make_qubits(3) - ps = PauliStringPhasor(PauliString({q2: Pauli.Z, - q1: Pauli.Y, - q0: Pauli.X}, False)) ** 0.5 + ps = PauliStringPhasor(cirq.PauliString({q2: cirq.Pauli.Z, + q1: cirq.Pauli.Y, + q0: cirq.Pauli.X}, False) + ) ** 0.5 assert (str(ps) == '{+, q0:X, q1:Y, q2:Z}**0.5') - ps = PauliStringPhasor(PauliString({q2: Pauli.Z, - q1: Pauli.Y, - q0: Pauli.X}, True)) ** -0.5 + ps = PauliStringPhasor(cirq.PauliString({q2: cirq.Pauli.Z, + q1: cirq.Pauli.Y, + q0: cirq.Pauli.X}, True)) ** -0.5 assert (str(ps) == '{-, q0:X, q1:Y, q2:Z}**-0.5') diff --git a/cirq/contrib/paulistring/pauli_string_raw_types.py b/cirq/contrib/paulistring/pauli_string_raw_types.py index 94abfcbd1db..1c98af89296 100644 --- a/cirq/contrib/paulistring/pauli_string_raw_types.py +++ b/cirq/contrib/paulistring/pauli_string_raw_types.py @@ -18,7 +18,7 @@ from cirq import ops, abc -from cirq.contrib.paulistring import PauliString +from cirq.ops.pauli_string import PauliString TSelf_PauliStringGateOperation = TypeVar('TSelf_PauliStringGateOperation', diff --git a/cirq/contrib/paulistring/pauli_string_raw_types_test.py b/cirq/contrib/paulistring/pauli_string_raw_types_test.py index b79a17b30b3..343c3d0cde6 100644 --- a/cirq/contrib/paulistring/pauli_string_raw_types_test.py +++ b/cirq/contrib/paulistring/pauli_string_raw_types_test.py @@ -16,11 +16,7 @@ import cirq -from cirq.contrib.paulistring import ( - Pauli, - PauliString, - PauliStringGateOperation, -) +from cirq.contrib.paulistring import PauliStringGateOperation def _make_qubits(n): @@ -44,7 +40,8 @@ def map_qubits(self, qubit_map): ps = self.pauli_string.map_qubits(qubit_map) return ValiGate(ps) - g = ValiGate(PauliString({q0: Pauli.X, q1: Pauli.Y, q2: Pauli.Z})) + g = ValiGate(cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y, + q2: cirq.Pauli.Z})) _ = g.with_qubits(q1, q0, q2) with pytest.raises(ValidError): @@ -59,7 +56,7 @@ def map_qubits(self, qubit_map): ps = self.pauli_string.map_qubits(qubit_map) return DummyGate(ps) - g = DummyGate(PauliString({q0: Pauli.X, q1: Pauli.Y})) + g = DummyGate(cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y})) _ = g.with_qubits(q1, q2) with pytest.raises(ValueError): @@ -79,7 +76,8 @@ def text_diagram_info(self, args: cirq.TextDiagramInfoArgs return self._pauli_string_diagram_info(args) q0, q1, q2 = _make_qubits(3) - ps = PauliString({q0: Pauli.X, q1: Pauli.Y, q2: Pauli.Z}) + ps = cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y, + q2: cirq.Pauli.Z}) circuit = cirq.Circuit.from_ops( DiagramGate(ps), diff --git a/cirq/ops/__init__.py b/cirq/ops/__init__.py index 7a71f2ddd14..47f44bb5ffa 100644 --- a/cirq/ops/__init__.py +++ b/cirq/ops/__init__.py @@ -83,6 +83,19 @@ freeze_op_tree, transform_op_tree, ) +from cirq.ops.pauli import ( + Pauli, +) +from cirq.ops.clifford_gate import ( + CliffordGate, + PauliTransform, +) +from cirq.ops.pauli_interaction_gate import ( + PauliInteractionGate, +) +from cirq.ops.pauli_string import ( + PauliString, +) from cirq.ops.raw_types import ( Gate, NamedQubit, diff --git a/cirq/ops/clifford_gate.py b/cirq/ops/clifford_gate.py index 67861faf12e..d4ce3d3012a 100644 --- a/cirq/ops/clifford_gate.py +++ b/cirq/ops/clifford_gate.py @@ -15,18 +15,17 @@ from typing import (Any, Dict, NamedTuple, Optional, Sequence, Tuple, Union, cast) -from cirq import ops - -from cirq.contrib.paulistring.pauli import Pauli +from cirq.ops import raw_types, gate_features, common_gates, op_tree +from cirq.ops.pauli import Pauli PauliTransform = NamedTuple('PauliTransform', [('to', Pauli), ('flip', bool)]) -class CliffordGate(ops.Gate, - ops.CompositeGate, - ops.ReversibleEffect, - ops.TextDiagrammable): +class CliffordGate(raw_types.Gate, + gate_features.CompositeGate, + gate_features.ReversibleEffect, + gate_features.TextDiagrammable): """Any single qubit Clifford rotation.""" I = None # type: CliffordGate H = None # type: CliffordGate @@ -234,14 +233,15 @@ def merged_with(self, second: 'CliffordGate') -> 'CliffordGate': return CliffordGate.from_xz_map((x_final_pauli, x_flip1 ^ x_flip2), (z_final_pauli, z_flip1 ^ z_flip2)) - def default_decompose(self, qubits: Sequence[ops.QubitId]) -> ops.OP_TREE: + def default_decompose(self, qubits: Sequence[raw_types.QubitId] + ) -> op_tree.OP_TREE: qubit, = qubits if self == CliffordGate.H: - return ops.H(qubit), + return common_gates.H(qubit), rotations = self.decompose_rotation() - pauli_gate_map = {Pauli.X: ops.X, - Pauli.Y: ops.Y, - Pauli.Z: ops.Z} + pauli_gate_map = {Pauli.X: common_gates.X, + Pauli.Y: common_gates.Y, + Pauli.Z: common_gates.Z} return tuple((pauli_gate_map[r](qubit) ** (qt / 2) for r, qt in rotations)) @@ -308,8 +308,8 @@ def __repr__(self): '+-'[self.transform(Pauli.Y).flip], self.transform(Pauli.Y).to, '+-'[self.transform(Pauli.Z).flip], self.transform(Pauli.Z).to) - def text_diagram_info(self, args: ops.TextDiagramInfoArgs - ) -> ops.TextDiagramInfo: + def text_diagram_info(self, args: gate_features.TextDiagramInfoArgs + ) -> gate_features.TextDiagramInfo: well_known_map = { CliffordGate.I: 'I', CliffordGate.H: 'H', @@ -330,7 +330,7 @@ def text_diagram_info(self, args: ops.TextDiagramInfoArgs symbol = '-'.join( str(r) + ('^' + str(qt / 2)) * (qt % 4 != 2) for r, qt in rotations) - return ops.TextDiagramInfo( + return gate_features.TextDiagramInfo( wire_symbols=(symbol,), exponent={ CliffordGate.X_sqrt: 0.5, diff --git a/cirq/ops/clifford_gate_test.py b/cirq/ops/clifford_gate_test.py index 99eba9bd37b..45fd3b14bfd 100644 --- a/cirq/ops/clifford_gate_test.py +++ b/cirq/ops/clifford_gate_test.py @@ -21,64 +21,58 @@ import cirq -from cirq.contrib.paulistring import Pauli -from cirq.contrib.paulistring import ( - CliffordGate, - PauliTransform, -) - _bools = (False, True) def _assert_not_mirror(gate) -> None: - trans_x = gate.transform(Pauli.X) - trans_y = gate.transform(Pauli.Y) - trans_z = gate.transform(Pauli.Z) + trans_x = gate.transform(cirq.Pauli.X) + trans_y = gate.transform(cirq.Pauli.Y) + trans_z = gate.transform(cirq.Pauli.Z) right_handed = (trans_x.flip ^ trans_y.flip ^ trans_z.flip ^ (trans_x.to - trans_y.to != 1)) assert right_handed, 'Mirrors' def _assert_no_collision(gate) -> None: - trans_x = gate.transform(Pauli.X) - trans_y = gate.transform(Pauli.Y) - trans_z = gate.transform(Pauli.Z) + trans_x = gate.transform(cirq.Pauli.X) + trans_y = gate.transform(cirq.Pauli.Y) + trans_z = gate.transform(cirq.Pauli.Z) assert trans_x.to != trans_y.to, 'Collision' assert trans_y.to != trans_z.to, 'Collision' assert trans_z.to != trans_x.to, 'Collision' def _all_rotations(): - for pauli, flip, in itertools.product(Pauli.XYZ, _bools): - yield PauliTransform(pauli, flip) + for pauli, flip, in itertools.product(cirq.Pauli.XYZ, _bools): + yield cirq.PauliTransform(pauli, flip) def _all_rotation_pairs(): - for px, flip_x, pz, flip_z in itertools.product(Pauli.XYZ, _bools, - Pauli.XYZ, _bools): + for px, flip_x, pz, flip_z in itertools.product(cirq.Pauli.XYZ, _bools, + cirq.Pauli.XYZ, _bools): if px == pz: continue - yield PauliTransform(px, flip_x), PauliTransform(pz, flip_z) + yield cirq.PauliTransform(px, flip_x), cirq.PauliTransform(pz, flip_z) def _all_clifford_gates(): for trans_x, trans_z in _all_rotation_pairs(): - yield CliffordGate.from_xz_map(trans_x, trans_z) + yield cirq.CliffordGate.from_xz_map(trans_x, trans_z) @pytest.mark.parametrize('pauli,flip_x,flip_z', - itertools.product(Pauli.XYZ, _bools, _bools)) + itertools.product(cirq.Pauli.XYZ, _bools, _bools)) def test_init_value_error(pauli, flip_x, flip_z): with pytest.raises(ValueError): - CliffordGate.from_xz_map((pauli, flip_x), (pauli, flip_z)) + cirq.CliffordGate.from_xz_map((pauli, flip_x), (pauli, flip_z)) @pytest.mark.parametrize('trans_x,trans_z', _all_rotation_pairs()) def test_init_from_xz(trans_x, trans_z): - gate = CliffordGate.from_xz_map(trans_x, trans_z) - assert gate.transform(Pauli.X) == trans_x - assert gate.transform(Pauli.Z) == trans_z + gate = cirq.CliffordGate.from_xz_map(trans_x, trans_z) + assert gate.transform(cirq.Pauli.X) == trans_x + assert gate.transform(cirq.Pauli.Z) == trans_z _assert_not_mirror(gate) _assert_no_collision(gate) @@ -87,14 +81,14 @@ def test_init_from_xz(trans_x, trans_z): ((trans1, trans2, from1) for trans1, trans2, from1 in itertools.product(_all_rotations(), _all_rotations(), - Pauli.XYZ) + cirq.Pauli.XYZ) if trans1.to != trans2.to)) def test_init_from_double_map_vs_kwargs(trans1, trans2, from1): from2 = from1 + 1 from1_str, from2_str = (str(frm).lower()+'_to' for frm in (from1, from2)) - gate_kw = CliffordGate.from_double_map(**{from1_str: trans1, - from2_str: trans2}) - gate_map = CliffordGate.from_double_map({from1: trans1, from2: trans2}) + gate_kw = cirq.CliffordGate.from_double_map(**{from1_str: trans1, + from2_str: trans2}) + gate_map = cirq.CliffordGate.from_double_map({from1: trans1, from2: trans2}) # Test initializes the same gate assert gate_kw == gate_map @@ -103,24 +97,24 @@ def test_init_from_double_map_vs_kwargs(trans1, trans2, from1): ((trans1, trans2, from1) for trans1, trans2, from1 in itertools.product(_all_rotations(), _all_rotations(), - Pauli.XYZ) + cirq.Pauli.XYZ) if trans1.to == trans2.to)) def test_init_from_double_invalid(trans1, trans2, from1): from2 = from1 + 1 # Test throws on invalid arguments with pytest.raises(ValueError): - CliffordGate.from_double_map({from1: trans1, from2: trans2}) + cirq.CliffordGate.from_double_map({from1: trans1, from2: trans2}) @pytest.mark.parametrize('trans1,trans2,from1', ((trans1, trans2, from1) for trans1, trans2, from1 in itertools.product(_all_rotations(), _all_rotations(), - Pauli.XYZ) + cirq.Pauli.XYZ) if trans1.to != trans2.to)) def test_init_from_double(trans1, trans2, from1): from2 = from1 + 1 - gate = CliffordGate.from_double_map({from1: trans1, from2: trans2}) + gate = cirq.CliffordGate.from_double_map({from1: trans1, from2: trans2}) # Test initializes what was expected assert gate.transform(from1) == trans1 assert gate.transform(from2) == trans2 @@ -130,21 +124,21 @@ def test_init_from_double(trans1, trans2, from1): @pytest.mark.parametrize('trans,frm', itertools.product(_all_rotations(), - Pauli.XYZ)) + cirq.Pauli.XYZ)) def test_init_from_single_map_vs_kwargs(trans, frm): from_str = str(frm).lower()+'_to' # pylint: disable=unexpected-keyword-arg - gate_kw = CliffordGate.from_single_map(**{from_str: trans}) - gate_map = CliffordGate.from_single_map({frm: trans}) + gate_kw = cirq.CliffordGate.from_single_map(**{from_str: trans}) + gate_map = cirq.CliffordGate.from_single_map({frm: trans}) assert gate_kw == gate_map @pytest.mark.parametrize('trans,frm', ((trans, frm) - for trans, frm in itertools.product(_all_rotations(), Pauli.XYZ) + for trans, frm in itertools.product(_all_rotations(), cirq.Pauli.XYZ) if trans.to != frm)) def test_init_90rot_from_single(trans, frm): - gate = CliffordGate.from_single_map({frm: trans}) + gate = cirq.CliffordGate.from_single_map({frm: trans}) assert gate.transform(frm) == trans _assert_not_mirror(gate) _assert_no_collision(gate) @@ -152,127 +146,127 @@ def test_init_90rot_from_single(trans, frm): assert len(gate.decompose_rotation()) == 1 # Check that this is a 90 degree rotation gate assert (gate.merged_with(gate).merged_with(gate).merged_with(gate) - == CliffordGate.I) + == cirq.CliffordGate.I) # Check that flipping the transform produces the inverse rotation - trans_rev = PauliTransform(trans.to, not trans.flip) - gate_rev = CliffordGate.from_single_map({frm: trans_rev}) + trans_rev = cirq.PauliTransform(trans.to, not trans.flip) + gate_rev = cirq.CliffordGate.from_single_map({frm: trans_rev}) assert gate.inverse() == gate_rev @pytest.mark.parametrize('trans,frm', ((trans, frm) - for trans, frm in itertools.product(_all_rotations(), Pauli.XYZ) + for trans, frm in itertools.product(_all_rotations(), cirq.Pauli.XYZ) if trans.to == frm and trans.flip)) def test_init_180rot_from_single(trans, frm): - gate = CliffordGate.from_single_map({frm: trans}) + gate = cirq.CliffordGate.from_single_map({frm: trans}) assert gate.transform(frm) == trans _assert_not_mirror(gate) _assert_no_collision(gate) # Check that it decomposes to one gate assert len(gate.decompose_rotation()) == 1 # Check that this is a 180 degree rotation gate - assert gate.merged_with(gate) == CliffordGate.I + assert gate.merged_with(gate) == cirq.CliffordGate.I @pytest.mark.parametrize('trans,frm', ((trans, frm) - for trans, frm in itertools.product(_all_rotations(), Pauli.XYZ) + for trans, frm in itertools.product(_all_rotations(), cirq.Pauli.XYZ) if trans.to == frm and not trans.flip)) def test_init_ident_from_single(trans, frm): - gate = CliffordGate.from_single_map({frm: trans}) + gate = cirq.CliffordGate.from_single_map({frm: trans}) assert gate.transform(frm) == trans _assert_not_mirror(gate) _assert_no_collision(gate) # Check that it decomposes to zero gates assert len(gate.decompose_rotation()) == 0 # Check that this is an identity gate - assert gate == CliffordGate.I + assert gate == cirq.CliffordGate.I @pytest.mark.parametrize('pauli,sqrt,expected', ( - (Pauli.X, False, CliffordGate.X), - (Pauli.Y, False, CliffordGate.Y), - (Pauli.Z, False, CliffordGate.Z), - (Pauli.X, True, CliffordGate.X_sqrt), - (Pauli.Y, True, CliffordGate.Y_sqrt), - (Pauli.Z, True, CliffordGate.Z_sqrt))) + (cirq.Pauli.X, False, cirq.CliffordGate.X), + (cirq.Pauli.Y, False, cirq.CliffordGate.Y), + (cirq.Pauli.Z, False, cirq.CliffordGate.Z), + (cirq.Pauli.X, True, cirq.CliffordGate.X_sqrt), + (cirq.Pauli.Y, True, cirq.CliffordGate.Y_sqrt), + (cirq.Pauli.Z, True, cirq.CliffordGate.Z_sqrt))) def test_init_from_pauli(pauli, sqrt, expected): - gate = CliffordGate.from_pauli(pauli, sqrt=sqrt) + gate = cirq.CliffordGate.from_pauli(pauli, sqrt=sqrt) assert gate == expected def test_init_invalid(): with pytest.raises(ValueError): - CliffordGate.from_single_map() + cirq.CliffordGate.from_single_map() with pytest.raises(ValueError): - CliffordGate.from_single_map({}) + cirq.CliffordGate.from_single_map({}) with pytest.raises(ValueError): - CliffordGate.from_single_map({Pauli.X: (Pauli.X, False)}, - y_to=(Pauli.Y, False)) + cirq.CliffordGate.from_single_map({cirq.Pauli.X: (cirq.Pauli.X, False)}, + y_to=(cirq.Pauli.Y, False)) with pytest.raises(ValueError): - CliffordGate.from_single_map({Pauli.X: (Pauli.X, False), - Pauli.Y: (Pauli.Y, False)}) + cirq.CliffordGate.from_single_map({cirq.Pauli.X: (cirq.Pauli.X, False), + cirq.Pauli.Y: (cirq.Pauli.Y, False)}) with pytest.raises(ValueError): - CliffordGate.from_double_map() + cirq.CliffordGate.from_double_map() with pytest.raises(ValueError): - CliffordGate.from_double_map({}) + cirq.CliffordGate.from_double_map({}) with pytest.raises(ValueError): - CliffordGate.from_double_map({Pauli.X: (Pauli.X, False)}) + cirq.CliffordGate.from_double_map({cirq.Pauli.X: (cirq.Pauli.X, False)}) with pytest.raises(ValueError): - CliffordGate.from_double_map(x_to=(Pauli.X, False)) + cirq.CliffordGate.from_double_map(x_to=(cirq.Pauli.X, False)) with pytest.raises(ValueError): - CliffordGate.from_single_map({Pauli.X: (Pauli.Y, False), - Pauli.Y: (Pauli.Z, False), - Pauli.Z: (Pauli.X, False)}) + cirq.CliffordGate.from_single_map({cirq.Pauli.X: (cirq.Pauli.Y, False), + cirq.Pauli.Y: (cirq.Pauli.Z, False), + cirq.Pauli.Z: (cirq.Pauli.X, False)}) with pytest.raises(ValueError): - CliffordGate.from_single_map({Pauli.X: (Pauli.X, False), - Pauli.Y: (Pauli.X, False)}) + cirq.CliffordGate.from_single_map({cirq.Pauli.X: (cirq.Pauli.X, False), + cirq.Pauli.Y: (cirq.Pauli.X, False)}) def test_eq_ne_and_hash(): eq = EqualsTester() for trans_x, trans_z in _all_rotation_pairs(): - gate_gen = lambda: CliffordGate.from_xz_map(trans_x, trans_z) + gate_gen = lambda: cirq.CliffordGate.from_xz_map(trans_x, trans_z) eq.make_equality_group(gate_gen) @pytest.mark.parametrize('gate,rep', ( - (CliffordGate.I, 'CliffordGate(X:+X, Y:+Y, Z:+Z)'), - (CliffordGate.H, 'CliffordGate(X:+Z, Y:-Y, Z:+X)'), - (CliffordGate.X, 'CliffordGate(X:+X, Y:-Y, Z:-Z)'), - (CliffordGate.X_sqrt, 'CliffordGate(X:+X, Y:+Z, Z:-Y)'))) + (cirq.CliffordGate.I, 'CliffordGate(X:+X, Y:+Y, Z:+Z)'), + (cirq.CliffordGate.H, 'CliffordGate(X:+Z, Y:-Y, Z:+X)'), + (cirq.CliffordGate.X, 'CliffordGate(X:+X, Y:-Y, Z:-Z)'), + (cirq.CliffordGate.X_sqrt, 'CliffordGate(X:+X, Y:+Z, Z:-Y)'))) def test_repr(gate, rep): assert repr(gate) == rep @pytest.mark.parametrize('gate,trans_y', ( - (CliffordGate.I, (Pauli.Y, False)), - (CliffordGate.H, (Pauli.Y, True)), - (CliffordGate.X, (Pauli.Y, True)), - (CliffordGate.Y, (Pauli.Y, False)), - (CliffordGate.Z, (Pauli.Y, True)), - (CliffordGate.X_sqrt, (Pauli.Z, False)), - (CliffordGate.X_nsqrt, (Pauli.Z, True)), - (CliffordGate.Y_sqrt, (Pauli.Y, False)), - (CliffordGate.Y_nsqrt, (Pauli.Y, False)), - (CliffordGate.Z_sqrt, (Pauli.X, True)), - (CliffordGate.Z_nsqrt, (Pauli.X, False)))) + (cirq.CliffordGate.I, (cirq.Pauli.Y, False)), + (cirq.CliffordGate.H, (cirq.Pauli.Y, True)), + (cirq.CliffordGate.X, (cirq.Pauli.Y, True)), + (cirq.CliffordGate.Y, (cirq.Pauli.Y, False)), + (cirq.CliffordGate.Z, (cirq.Pauli.Y, True)), + (cirq.CliffordGate.X_sqrt, (cirq.Pauli.Z, False)), + (cirq.CliffordGate.X_nsqrt, (cirq.Pauli.Z, True)), + (cirq.CliffordGate.Y_sqrt, (cirq.Pauli.Y, False)), + (cirq.CliffordGate.Y_nsqrt, (cirq.Pauli.Y, False)), + (cirq.CliffordGate.Z_sqrt, (cirq.Pauli.X, True)), + (cirq.CliffordGate.Z_nsqrt, (cirq.Pauli.X, False)))) def test_y_rotation(gate, trans_y): - assert gate.transform(Pauli.Y) == trans_y + assert gate.transform(cirq.Pauli.Y) == trans_y @pytest.mark.parametrize('gate,gate_equiv', ( - (CliffordGate.I, cirq.X ** 0), - (CliffordGate.H, cirq.H), - (CliffordGate.X, cirq.X), - (CliffordGate.Y, cirq.Y), - (CliffordGate.Z, cirq.Z), - (CliffordGate.X_sqrt, cirq.X ** 0.5), - (CliffordGate.X_nsqrt, cirq.X ** -0.5), - (CliffordGate.Y_sqrt, cirq.Y ** 0.5), - (CliffordGate.Y_nsqrt, cirq.Y ** -0.5), - (CliffordGate.Z_sqrt, cirq.Z ** 0.5), - (CliffordGate.Z_nsqrt, cirq.Z ** -0.5))) + (cirq.CliffordGate.I, cirq.X ** 0), + (cirq.CliffordGate.H, cirq.H), + (cirq.CliffordGate.X, cirq.X), + (cirq.CliffordGate.Y, cirq.Y), + (cirq.CliffordGate.Z, cirq.Z), + (cirq.CliffordGate.X_sqrt, cirq.X ** 0.5), + (cirq.CliffordGate.X_nsqrt, cirq.X ** -0.5), + (cirq.CliffordGate.Y_sqrt, cirq.Y ** 0.5), + (cirq.CliffordGate.Y_nsqrt, cirq.Y ** -0.5), + (cirq.CliffordGate.Z_sqrt, cirq.Z ** 0.5), + (cirq.CliffordGate.Z_nsqrt, cirq.Z ** -0.5))) def test_decompose(gate, gate_equiv): q0 = cirq.NamedQubit('q0') mat = cirq.Circuit.from_ops( @@ -317,12 +311,12 @@ def test_commutes_with_single_qubit_gate(gate, other): @pytest.mark.parametrize('gate,pauli,half_turns', itertools.product(_all_clifford_gates(), - Pauli.XYZ, + cirq.Pauli.XYZ, (0.1, 0.25, 0.5, -0.5))) def test_commutes_with_pauli(gate, pauli, half_turns): - pauli_gates = {Pauli.X: cirq.X, - Pauli.Y: cirq.Y, - Pauli.Z: cirq.Z} + pauli_gates = {cirq.Pauli.X: cirq.X, + cirq.Pauli.Y: cirq.Y, + cirq.Pauli.Z: cirq.Z} pauli_gate = pauli_gates[pauli] ** half_turns q0 = cirq.NamedQubit('q0') mat = cirq.Circuit.from_ops( @@ -355,15 +349,16 @@ def test_single_qubit_gate_after_switching_order(gate, other): @pytest.mark.parametrize('gate,sym,exp', ( - (CliffordGate.I, 'I', 1), - (CliffordGate.H, 'H', 1), - (CliffordGate.X, 'X', 1), - (CliffordGate.X_sqrt, 'X', 0.5), - (CliffordGate.X_nsqrt, 'X', -0.5), + (cirq.CliffordGate.I, 'I', 1), + (cirq.CliffordGate.H, 'H', 1), + (cirq.CliffordGate.X, 'X', 1), + (cirq.CliffordGate.X_sqrt, 'X', 0.5), + (cirq.CliffordGate.X_nsqrt, 'X', -0.5), ( - CliffordGate.from_xz_map((Pauli.Y, False), (Pauli.X, True)), - 'X^-0.5-Z^0.5', - 1 + cirq.CliffordGate.from_xz_map( + (cirq.Pauli.Y, False), (cirq.Pauli.X, True)), + 'X^-0.5-Z^0.5', + 1 ))) def test_text_diagram_info(gate, sym, exp): assert gate.text_diagram_info( diff --git a/cirq/contrib/paulistring/pauli_interaction_gate.py b/cirq/ops/pauli_interaction_gate.py similarity index 85% rename from cirq/contrib/paulistring/pauli_interaction_gate.py rename to cirq/ops/pauli_interaction_gate.py index ff7930b8ac9..3b778309775 100644 --- a/cirq/contrib/paulistring/pauli_interaction_gate.py +++ b/cirq/ops/pauli_interaction_gate.py @@ -16,9 +16,10 @@ import numpy as np -from cirq import ops, value - -from cirq.contrib.paulistring import Pauli, CliffordGate +from cirq import value +from cirq.ops import raw_types, gate_features, common_gates, eigen_gate, op_tree +from cirq.ops.pauli import Pauli +from cirq.ops.clifford_gate import CliffordGate pauli_eigen_map = { @@ -31,10 +32,10 @@ } -class PauliInteractionGate(ops.EigenGate, - ops.CompositeGate, - ops.InterchangeableQubitsGate, - ops.TextDiagrammable): +class PauliInteractionGate(eigen_gate.EigenGate, + gate_features.CompositeGate, + gate_features.InterchangeableQubitsGate, + gate_features.TextDiagrammable): def __init__(self, pauli0: Pauli, invert0: bool, pauli1: Pauli, invert1: bool, @@ -99,8 +100,8 @@ def _eigen_components(self) -> List[Tuple[float, np.ndarray]]: comp0 = np.eye(4) - comp1 return [(0, comp0), (1, comp1)] - def default_decompose(self, qubits: Sequence[ops.QubitId] - ) -> ops.op_tree.OP_TREE: + def default_decompose(self, qubits: Sequence[raw_types.QubitId] + ) -> op_tree.OP_TREE: q0, q1 = qubits right_gate0 = CliffordGate.from_single_map( z_to=(self.pauli0, self.invert0)) @@ -110,19 +111,19 @@ def default_decompose(self, qubits: Sequence[ops.QubitId] left_gate1 = right_gate1.inverse() yield left_gate0(q0) yield left_gate1(q1) - yield ops.Rot11Gate(half_turns=self._exponent)(q0, q1) + yield common_gates.Rot11Gate(half_turns=self._exponent)(q0, q1) yield right_gate0(q0) yield right_gate1(q1) - def text_diagram_info(self, args: ops.TextDiagramInfoArgs - ) -> ops.TextDiagramInfo: + def text_diagram_info(self, args: gate_features.TextDiagramInfoArgs + ) -> gate_features.TextDiagramInfo: labels = {Pauli.X: 'X', Pauli.Y: 'Y', Pauli.Z: '@'} l0 = labels[self.pauli0] l1 = labels[self.pauli1] # Add brackets around letter if inverted l0, l1 = ('(-{})'.format(l) if inv else l for l, inv in ((l0, self.invert0), (l1, self.invert1))) - return ops.TextDiagramInfo( + return gate_features.TextDiagramInfo( wire_symbols=(l0, l1), exponent=self._exponent) diff --git a/cirq/contrib/paulistring/pauli_interaction_gate_test.py b/cirq/ops/pauli_interaction_gate_test.py similarity index 62% rename from cirq/contrib/paulistring/pauli_interaction_gate_test.py rename to cirq/ops/pauli_interaction_gate_test.py index 2e0e6fdcaf2..2502fa00111 100644 --- a/cirq/contrib/paulistring/pauli_interaction_gate_test.py +++ b/cirq/ops/pauli_interaction_gate_test.py @@ -22,31 +22,26 @@ import cirq -from cirq.contrib.paulistring import ( - Pauli, - PauliInteractionGate, -) - _bools = (False, True) def _all_interaction_gates(half_turns_list=(1,)): for pauli0, invert0, pauli1, invert1, half_turns in itertools.product( - Pauli.XYZ, _bools, - Pauli.XYZ, _bools, + cirq.Pauli.XYZ, _bools, + cirq.Pauli.XYZ, _bools, half_turns_list): - yield PauliInteractionGate(pauli0, invert0, - pauli1, invert1, - half_turns=half_turns) + yield cirq.PauliInteractionGate(pauli0, invert0, + pauli1, invert1, + half_turns=half_turns) def test_eq_ne_and_hash(): eq = EqualsTester() for pauli0, invert0, pauli1, invert1, half_turns in itertools.product( - Pauli.XYZ, _bools, - Pauli.XYZ, _bools, + cirq.Pauli.XYZ, _bools, + cirq.Pauli.XYZ, _bools, (0.1, -0.25, 1)): - gate_gen = lambda offset: PauliInteractionGate( + gate_gen = lambda offset: cirq.PauliInteractionGate( pauli0, invert0, pauli1, invert1, half_turns=half_turns + offset) @@ -81,7 +76,7 @@ def test_decompose(gate): assert_allclose_up_to_global_phase(decompose_mat, gate_mat) def test_exponent(): - cnot = PauliInteractionGate(Pauli.Z, False, Pauli.X, False) + cnot = cirq.PauliInteractionGate(cirq.Pauli.Z, False, cirq.Pauli.X, False) np.testing.assert_almost_equal( (cnot**0.5).matrix(), np.array([ @@ -102,8 +97,8 @@ def test_exponent(): def test_decomposes_despite_symbol(): q0, q1 = cirq.NamedQubit('q0'), cirq.NamedQubit('q1') - gate = PauliInteractionGate(Pauli.Z, False, Pauli.X, False, - half_turns=cirq.Symbol('x')) + gate = cirq.PauliInteractionGate(cirq.Pauli.Z, False, cirq.Pauli.X, False, + half_turns=cirq.Symbol('x')) op_tree = gate.default_decompose([q0, q1]) ops = tuple(cirq.flatten_op_tree(op_tree)) assert ops @@ -111,15 +106,24 @@ def test_decomposes_despite_symbol(): def test_text_diagrams(): q0, q1 = cirq.NamedQubit('q0'), cirq.NamedQubit('q1') circuit = cirq.Circuit.from_ops( - PauliInteractionGate(Pauli.X, False, Pauli.X, False)(q0, q1), - PauliInteractionGate(Pauli.X, True, Pauli.X, False)(q0, q1), - PauliInteractionGate(Pauli.X, False, Pauli.X, True )(q0, q1), - PauliInteractionGate(Pauli.X, True, Pauli.X, True )(q0, q1), - PauliInteractionGate(Pauli.X, False, Pauli.Y, False)(q0, q1), - PauliInteractionGate(Pauli.Y, False, Pauli.Z, False)(q0, q1), - PauliInteractionGate(Pauli.Z, False, Pauli.Y, False)(q0, q1), - PauliInteractionGate(Pauli.Y, True, Pauli.Z, True )(q0, q1), - PauliInteractionGate(Pauli.Z, True, Pauli.Y, True )(q0, q1)) + cirq.PauliInteractionGate(cirq.Pauli.X, False, + cirq.Pauli.X, False)(q0, q1), + cirq.PauliInteractionGate(cirq.Pauli.X, True, + cirq.Pauli.X, False)(q0, q1), + cirq.PauliInteractionGate(cirq.Pauli.X, False, + cirq.Pauli.X, True)(q0, q1), + cirq.PauliInteractionGate(cirq.Pauli.X, True, + cirq.Pauli.X, True)(q0, q1), + cirq.PauliInteractionGate(cirq.Pauli.X, False, + cirq.Pauli.Y, False)(q0, q1), + cirq.PauliInteractionGate(cirq.Pauli.Y, False, + cirq.Pauli.Z, False)(q0, q1), + cirq.PauliInteractionGate(cirq.Pauli.Z, False, + cirq.Pauli.Y, False)(q0, q1), + cirq.PauliInteractionGate(cirq.Pauli.Y, True, + cirq.Pauli.Z, True)(q0, q1), + cirq.PauliInteractionGate(cirq.Pauli.Z, True, + cirq.Pauli.Y, True)(q0, q1)) assert circuit.to_text_diagram().strip() == """ q0: ───X───(-X)───X──────(-X)───X───Y───@───(-Y)───(-@)─── │ │ │ │ │ │ │ │ │ @@ -127,23 +131,23 @@ def test_text_diagrams(): """.strip() @pytest.mark.parametrize('gate,gate_repr', ( - (PauliInteractionGate(Pauli.X, False, Pauli.X, False), + (cirq.PauliInteractionGate(cirq.Pauli.X, False, cirq.Pauli.X, False), 'PauliInteractionGate(+X, +X)'), - (PauliInteractionGate(Pauli.X, True, Pauli.X, False), + (cirq.PauliInteractionGate(cirq.Pauli.X, True, cirq.Pauli.X, False), 'PauliInteractionGate(-X, +X)'), - (PauliInteractionGate(Pauli.X, False, Pauli.X, True ), + (cirq.PauliInteractionGate(cirq.Pauli.X, False, cirq.Pauli.X, True ), 'PauliInteractionGate(+X, -X)'), - (PauliInteractionGate(Pauli.X, True, Pauli.X, True ), + (cirq.PauliInteractionGate(cirq.Pauli.X, True, cirq.Pauli.X, True ), 'PauliInteractionGate(-X, -X)'), - (PauliInteractionGate(Pauli.X, False, Pauli.Y, False), + (cirq.PauliInteractionGate(cirq.Pauli.X, False, cirq.Pauli.Y, False), 'PauliInteractionGate(+X, +Y)'), - (PauliInteractionGate(Pauli.Y, False, Pauli.Z, False), + (cirq.PauliInteractionGate(cirq.Pauli.Y, False, cirq.Pauli.Z, False), 'PauliInteractionGate(+Y, +Z)'), - (PauliInteractionGate(Pauli.Z, False, Pauli.Y, False), + (cirq.PauliInteractionGate(cirq.Pauli.Z, False, cirq.Pauli.Y, False), 'PauliInteractionGate(+Z, +Y)'), - (PauliInteractionGate(Pauli.Y, True, Pauli.Z, True ), + (cirq.PauliInteractionGate(cirq.Pauli.Y, True, cirq.Pauli.Z, True ), 'PauliInteractionGate(-Y, -Z)'), - (PauliInteractionGate(Pauli.Z, True, Pauli.Y, True ), + (cirq.PauliInteractionGate(cirq.Pauli.Z, True, cirq.Pauli.Y, True ), 'PauliInteractionGate(-Z, -Y)'))) def test_repr(gate, gate_repr): assert repr(gate) == gate_repr diff --git a/cirq/ops/pauli_string.py b/cirq/ops/pauli_string.py index e9f79737b3b..14dcad28fa7 100644 --- a/cirq/ops/pauli_string.py +++ b/cirq/ops/pauli_string.py @@ -15,20 +15,21 @@ from typing import (Any, Dict, ItemsView, Iterable, Iterator, KeysView, Mapping, Optional, Tuple, ValuesView) -from cirq import ops - -from cirq.contrib.paulistring import Pauli, CliffordGate, PauliInteractionGate +from cirq.ops import raw_types, gate_operation, qubit_order +from cirq.ops.pauli import Pauli +from cirq.ops.clifford_gate import CliffordGate +from cirq.ops.pauli_interaction_gate import PauliInteractionGate class PauliString: def __init__(self, - qubit_pauli_map: Mapping[ops.QubitId, Pauli], + qubit_pauli_map: Mapping[raw_types.QubitId, Pauli], negated: bool = False) -> None: self._qubit_pauli_map = dict(qubit_pauli_map) self.negated = negated @staticmethod - def from_single(qubit: ops.QubitId, pauli: Pauli) -> 'PauliString': + def from_single(qubit: raw_types.QubitId, pauli: Pauli) -> 'PauliString': """Creates a PauliString with a single qubit.""" return PauliString({qubit: pauli}) @@ -48,20 +49,20 @@ def __ne__(self, other): def __hash__(self): return hash((PauliString, self.negated, frozenset(self.items()))) - def __getitem__(self, key: ops.QubitId) -> Pauli: + def __getitem__(self, key: raw_types.QubitId) -> Pauli: return self._qubit_pauli_map[key] - def get(self, key: ops.QubitId, default: Optional[Pauli] = None + def get(self, key: raw_types.QubitId, default: Optional[Pauli] = None ) -> Optional[Pauli]: return self._qubit_pauli_map.get(key, default) - def __contains__(self, key: ops.QubitId) -> bool: + def __contains__(self, key: raw_types.QubitId) -> bool: return key in self._qubit_pauli_map - def keys(self) -> KeysView[ops.QubitId]: + def keys(self) -> KeysView[raw_types.QubitId]: return self._qubit_pauli_map.keys() - def qubits(self) -> KeysView[ops.QubitId]: + def qubits(self) -> KeysView[raw_types.QubitId]: return self.keys() def values(self) -> ValuesView[Pauli]: @@ -70,7 +71,7 @@ def values(self) -> ValuesView[Pauli]: def items(self) -> ItemsView: return self._qubit_pauli_map.items() - def __iter__(self) -> Iterator[ops.QubitId]: + def __iter__(self) -> Iterator[raw_types.QubitId]: return iter(self._qubit_pauli_map.keys()) def __len__(self) -> int: @@ -79,18 +80,18 @@ def __len__(self) -> int: def __repr__(self): map_str = ', '.join(('{!r}: {!r}'.format(qubit, self[qubit]) for qubit in - ops.QubitOrder.DEFAULT.order_for(self))) + qubit_order.QubitOrder.DEFAULT.order_for(self))) return 'PauliString({{{}}}, {})'.format(map_str, self.negated) def __str__(self): - ordered_qubits = ops.QubitOrder.DEFAULT.order_for(self.qubits()) + ordered_qubits = qubit_order.QubitOrder.DEFAULT.order_for(self.qubits()) return '{{{}, {}}}'.format('+-'[self.negated], ', '.join(('{!s}:{!s}'.format(q, self[q]) for q in ordered_qubits))) def zip_items(self, other: 'PauliString' - ) -> Iterator[Tuple[ops.QubitId, Tuple[Pauli, Pauli]]]: + ) -> Iterator[Tuple[raw_types.QubitId, Tuple[Pauli, Pauli]]]: for qubit, pauli0 in self.items(): if qubit in other: yield qubit, (pauli0, other[qubit]) @@ -112,14 +113,14 @@ def __neg__(self) -> 'PauliString': def __pos__(self) -> 'PauliString': return self - def map_qubits(self, qubit_map: Dict[ops.QubitId, ops.QubitId] + def map_qubits(self, qubit_map: Dict[raw_types.QubitId, raw_types.QubitId] ) -> 'PauliString': new_qubit_pauli_map = {qubit_map[qubit]: pauli for qubit, pauli in self.items()} return PauliString(new_qubit_pauli_map, self.negated) def pass_operations_over(self, - ops: Iterable[ops.Operation], + ops: Iterable[raw_types.Operation], after_to_before: bool = False) -> 'PauliString': """Return a new PauliString such that the circuits --op--...--op--self-- and --output--op--...--op-- @@ -147,10 +148,10 @@ def pass_operations_over(self, return PauliString(pauli_map, inv) @staticmethod - def _pass_operation_over(pauli_map: Dict[ops.QubitId, Pauli], - op: ops.Operation, + def _pass_operation_over(pauli_map: Dict[raw_types.QubitId, Pauli], + op: raw_types.Operation, after_to_before: bool = False) -> bool: - if isinstance(op, ops.GateOperation): + if isinstance(op, gate_operation.GateOperation): if isinstance(op.gate, CliffordGate): return PauliString._pass_single_clifford_gate_over( pauli_map, op.gate, op.qubits[0], @@ -162,9 +163,10 @@ def _pass_operation_over(pauli_map: Dict[ops.QubitId, Pauli], raise TypeError('Unsupported operation: {!r}'.format(op)) @staticmethod - def _pass_single_clifford_gate_over(pauli_map: Dict[ops.QubitId, Pauli], + def _pass_single_clifford_gate_over(pauli_map: Dict[raw_types.QubitId, + Pauli], gate: CliffordGate, - qubit: ops.QubitId, + qubit: raw_types.QubitId, after_to_before: bool = False) -> bool: if qubit not in pauli_map: return False @@ -175,10 +177,11 @@ def _pass_single_clifford_gate_over(pauli_map: Dict[ops.QubitId, Pauli], return inv @staticmethod - def _pass_pauli_interaction_gate_over(pauli_map: Dict[ops.QubitId, Pauli], + def _pass_pauli_interaction_gate_over(pauli_map: Dict[raw_types.QubitId, + Pauli], gate: PauliInteractionGate, - qubit0: ops.QubitId, - qubit1: ops.QubitId, + qubit0: raw_types.QubitId, + qubit1: raw_types.QubitId, after_to_before: bool = False ) -> bool: def merge_and_kickback(qubit, pauli_left, pauli_right, inv): diff --git a/cirq/ops/pauli_string_test.py b/cirq/ops/pauli_string_test.py index 115d30bf36e..041af297411 100644 --- a/cirq/ops/pauli_string_test.py +++ b/cirq/ops/pauli_string_test.py @@ -20,20 +20,13 @@ import cirq -from cirq.contrib.paulistring import ( - Pauli, - CliffordGate, - PauliInteractionGate, - PauliString, -) - def _make_qubits(n): return [cirq.NamedQubit('q{}'.format(i)) for i in range(n)] def _sample_qubit_pauli_maps(): qubits = _make_qubits(3) - paulis_or_none = (None,) + Pauli.XYZ + paulis_or_none = (None,) + cirq.Pauli.XYZ for paulis in itertools.product(paulis_or_none, repeat=len(qubits)): yield {qubit: pauli for qubit, pauli in zip(qubits, paulis) if pauli is not None} @@ -43,26 +36,28 @@ def test_eq_ne_hash(): q0, q1, q2 = _make_qubits(3) eq = EqualsTester() eq.make_equality_group( - lambda: PauliString({}), - lambda: PauliString({}, False)) - eq.add_equality_group(PauliString({}, True)) - for q, pauli in itertools.product((q0, q1), Pauli.XYZ): - eq.add_equality_group(PauliString({q: pauli}, False)) - eq.add_equality_group(PauliString({q: pauli}, True)) - for q, p0, p1 in itertools.product((q0, q1), Pauli.XYZ, Pauli.XYZ): - eq.add_equality_group(PauliString({q: p0, q2: p1}, False)) - - -@pytest.mark.parametrize('pauli', Pauli.XYZ) + lambda: cirq.PauliString({}), + lambda: cirq.PauliString({}, False)) + eq.add_equality_group(cirq.PauliString({}, True)) + for q, pauli in itertools.product((q0, q1), cirq.Pauli.XYZ): + eq.add_equality_group(cirq.PauliString({q: pauli}, False)) + eq.add_equality_group(cirq.PauliString({q: pauli}, True)) + for q, p0, p1 in itertools.product((q0, q1), cirq.Pauli.XYZ, + cirq.Pauli.XYZ): + eq.add_equality_group(cirq.PauliString({q: p0, q2: p1}, False)) + + +@pytest.mark.parametrize('pauli', cirq.Pauli.XYZ) def test_from_single(pauli): q0, = _make_qubits(1) - assert PauliString.from_single(q0, pauli) == PauliString({q0: pauli}) + assert (cirq.PauliString.from_single(q0, pauli) + == cirq.PauliString({q0: pauli})) @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps()) def test_getitem(qubit_pauli_map): other = cirq.NamedQubit('other') - pauli_string = PauliString(qubit_pauli_map) + pauli_string = cirq.PauliString(qubit_pauli_map) for key in qubit_pauli_map: assert qubit_pauli_map[key] == pauli_string[key] with pytest.raises(KeyError): @@ -74,7 +69,7 @@ def test_getitem(qubit_pauli_map): @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps()) def test_get(qubit_pauli_map): other = cirq.NamedQubit('other') - pauli_string = PauliString(qubit_pauli_map) + pauli_string = cirq.PauliString(qubit_pauli_map) for key in qubit_pauli_map: assert qubit_pauli_map.get(key) == pauli_string.get(key) assert qubit_pauli_map.get(other) == pauli_string.get(other) == None @@ -84,7 +79,7 @@ def test_get(qubit_pauli_map): @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps()) def test_contains(qubit_pauli_map): other = cirq.NamedQubit('other') - pauli_string = PauliString(qubit_pauli_map) + pauli_string = cirq.PauliString(qubit_pauli_map) for key in qubit_pauli_map: assert key in pauli_string assert other not in pauli_string @@ -92,7 +87,7 @@ def test_contains(qubit_pauli_map): @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps()) def test_keys(qubit_pauli_map): - pauli_string = PauliString(qubit_pauli_map) + pauli_string = cirq.PauliString(qubit_pauli_map) assert (len(qubit_pauli_map.keys()) == len(pauli_string.keys()) == len(pauli_string.qubits())) assert (set(qubit_pauli_map.keys()) == set(pauli_string.keys()) @@ -101,27 +96,27 @@ def test_keys(qubit_pauli_map): @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps()) def test_items(qubit_pauli_map): - pauli_string = PauliString(qubit_pauli_map) + pauli_string = cirq.PauliString(qubit_pauli_map) assert len(qubit_pauli_map.items()) == len(pauli_string.items()) assert set(qubit_pauli_map.items()) == set(pauli_string.items()) @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps()) def test_values(qubit_pauli_map): - pauli_string = PauliString(qubit_pauli_map) + pauli_string = cirq.PauliString(qubit_pauli_map) assert len(qubit_pauli_map.values()) == len(pauli_string.values()) assert set(qubit_pauli_map.values()) == set(pauli_string.values()) @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps()) def test_len(qubit_pauli_map): - pauli_string = PauliString(qubit_pauli_map) + pauli_string = cirq.PauliString(qubit_pauli_map) assert len(qubit_pauli_map) == len(pauli_string) @pytest.mark.parametrize('qubit_pauli_map', _sample_qubit_pauli_maps()) def test_iter(qubit_pauli_map): - pauli_string = PauliString(qubit_pauli_map) + pauli_string = cirq.PauliString(qubit_pauli_map) assert len(tuple(qubit_pauli_map)) == len(tuple(pauli_string)) assert set(tuple(qubit_pauli_map)) == set(tuple(pauli_string)) @@ -130,38 +125,45 @@ def test_iter(qubit_pauli_map): @cirq.testing.only_test_in_python3 def test_repr(): q0, q1, q2 = _make_qubits(3) - pauli_string = PauliString({q2: Pauli.X, q1: Pauli.Y, q0: Pauli.Z}) + pauli_string = cirq.PauliString({q2: cirq.Pauli.X, q1: cirq.Pauli.Y, + q0: cirq.Pauli.Z}) assert (repr(pauli_string) == "PauliString({NamedQubit('q0'): Pauli.Z, " - "NamedQubit('q1'): Pauli.Y, NamedQubit('q2'): Pauli.X}, False)") + "NamedQubit('q1'): Pauli.Y, NamedQubit('q2'): Pauli.X}, " + "False)") assert (repr(pauli_string.negate()) == "PauliString({NamedQubit('q0'): Pauli.Z, " - "NamedQubit('q1'): Pauli.Y, NamedQubit('q2'): Pauli.X}, True)") + "NamedQubit('q1'): Pauli.Y, NamedQubit('q2'): Pauli.X}, " + "True)") def test_str(): q0, q1, q2 = _make_qubits(3) - pauli_string = PauliString({q2: Pauli.X, q1: Pauli.Y, q0: Pauli.Z}) + pauli_string = cirq.PauliString({q2: cirq.Pauli.X, q1: cirq.Pauli.Y, + q0: cirq.Pauli.Z}) assert str(pauli_string) == '{+, q0:Z, q1:Y, q2:X}' assert str(pauli_string.negate()) == '{-, q0:Z, q1:Y, q2:X}' @pytest.mark.parametrize('map1,map2,out', (lambda q0, q1, q2: ( ({}, {}, {}), - ({q0: Pauli.X}, {q0: Pauli.Y}, - {q0: (Pauli.X, Pauli.Y)}), - ({q0: Pauli.X}, {q1: Pauli.X}, + ({q0: cirq.Pauli.X}, {q0: cirq.Pauli.Y}, + {q0: (cirq.Pauli.X, cirq.Pauli.Y)}), + ({q0: cirq.Pauli.X}, {q1: cirq.Pauli.X}, {}), - ({q0: Pauli.Y, q1: Pauli.Z}, {q1: Pauli.Y, q2: Pauli.X}, - {q1: (Pauli.Z, Pauli.Y)}), - ({q0: Pauli.X, q1: Pauli.Y, q2: Pauli.Z}, {}, + ({q0: cirq.Pauli.Y, q1: cirq.Pauli.Z}, + {q1: cirq.Pauli.Y, q2: cirq.Pauli.X}, + {q1: (cirq.Pauli.Z, cirq.Pauli.Y)}), + ({q0: cirq.Pauli.X, q1: cirq.Pauli.Y, q2: cirq.Pauli.Z}, {}, {}), - ({q0: Pauli.X, q1: Pauli.Y, q2: Pauli.Z}, {q0: Pauli.Y, q1: Pauli.Z}, - {q0: (Pauli.X, Pauli.Y), q1: (Pauli.Y, Pauli.Z)}), + ({q0: cirq.Pauli.X, q1: cirq.Pauli.Y, q2: cirq.Pauli.Z}, + {q0: cirq.Pauli.Y, q1: cirq.Pauli.Z}, + {q0: (cirq.Pauli.X, cirq.Pauli.Y), + q1: (cirq.Pauli.Y, cirq.Pauli.Z)}), ))(*_make_qubits(3))) def test_zip_items(map1, map2, out): - ps1 = PauliString(map1) - ps2 = PauliString(map2) + ps1 = cirq.PauliString(map1) + ps2 = cirq.PauliString(map2) out_actual = tuple(ps1.zip_items(ps2)) assert len(out_actual) == len(out) assert dict(out_actual) == out @@ -169,20 +171,23 @@ def test_zip_items(map1, map2, out): @pytest.mark.parametrize('map1,map2,out', (lambda q0, q1, q2: ( ({}, {}, ()), - ({q0: Pauli.X}, {q0: Pauli.Y}, - ((Pauli.X, Pauli.Y),)), - ({q0: Pauli.X}, {q1: Pauli.X}, + ({q0: cirq.Pauli.X}, {q0: cirq.Pauli.Y}, + ((cirq.Pauli.X, cirq.Pauli.Y),)), + ({q0: cirq.Pauli.X}, {q1: cirq.Pauli.X}, ()), - ({q0: Pauli.Y, q1: Pauli.Z}, {q1: Pauli.Y, q2: Pauli.X}, - ((Pauli.Z, Pauli.Y),)), - ({q0: Pauli.X, q1: Pauli.Y, q2: Pauli.Z}, {}, + ({q0: cirq.Pauli.Y, q1: cirq.Pauli.Z}, + {q1: cirq.Pauli.Y, q2: cirq.Pauli.X}, + ((cirq.Pauli.Z, cirq.Pauli.Y),)), + ({q0: cirq.Pauli.X, q1: cirq.Pauli.Y, q2: cirq.Pauli.Z}, {}, ()), - ({q0: Pauli.X, q1: Pauli.Y, q2: Pauli.Z}, {q0: Pauli.Y, q1: Pauli.Z}, - ((Pauli.X, Pauli.Y), (Pauli.Y, Pauli.Z))) # Order not necessary + ({q0: cirq.Pauli.X, q1: cirq.Pauli.Y, q2: cirq.Pauli.Z}, + {q0: cirq.Pauli.Y, q1: cirq.Pauli.Z}, + # Order not necessary + ((cirq.Pauli.X, cirq.Pauli.Y), (cirq.Pauli.Y, cirq.Pauli.Z))) ))(*_make_qubits(3))) def test_zip_paulis(map1, map2, out): - ps1 = PauliString(map1) - ps2 = PauliString(map2) + ps1 = cirq.PauliString(map1) + ps2 = cirq.PauliString(map2) out_actual = tuple(ps1.zip_paulis(ps2)) assert len(out_actual) == len(out) if len(out) <= 1: @@ -193,48 +198,57 @@ def test_zip_paulis(map1, map2, out): def test_commutes_with(): q0, q1, q2 = _make_qubits(3) - assert PauliString.from_single(q0, Pauli.X).commutes_with( - PauliString.from_single(q0, Pauli.X)) - assert not PauliString.from_single(q0, Pauli.X).commutes_with( - PauliString.from_single(q0, Pauli.Y)) - assert PauliString.from_single(q0, Pauli.X).commutes_with( - PauliString.from_single(q1, Pauli.X)) - assert PauliString.from_single(q0, Pauli.X).commutes_with( - PauliString.from_single(q1, Pauli.Y)) - - assert PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q0: Pauli.X, q1: Pauli.Y})) - assert not PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q0: Pauli.X, q1: Pauli.Z})) - assert PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q0: Pauli.Y, q1: Pauli.X})) - assert PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q0: Pauli.Y, q1: Pauli.Z})) - - assert PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q0: Pauli.X, q1: Pauli.Y, q2: Pauli.Z})) - assert not PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q0: Pauli.X, q1: Pauli.Z, q2: Pauli.Z})) - assert PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q0: Pauli.Y, q1: Pauli.X, q2: Pauli.Z})) - assert PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q0: Pauli.Y, q1: Pauli.Z, q2: Pauli.X})) - - assert PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q2: Pauli.X, q1: Pauli.Y})) - assert not PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q2: Pauli.X, q1: Pauli.Z})) - assert not PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q2: Pauli.Y, q1: Pauli.X})) - assert not PauliString({q0: Pauli.X, q1: Pauli.Y}).commutes_with( - PauliString({q2: Pauli.Y, q1: Pauli.Z})) + assert cirq.PauliString.from_single(q0, cirq.Pauli.X).commutes_with( + cirq.PauliString.from_single(q0, cirq.Pauli.X)) + assert not cirq.PauliString.from_single(q0, cirq.Pauli.X).commutes_with( + cirq.PauliString.from_single(q0, cirq.Pauli.Y)) + assert cirq.PauliString.from_single(q0, cirq.Pauli.X).commutes_with( + cirq.PauliString.from_single(q1, cirq.Pauli.X)) + assert cirq.PauliString.from_single(q0, cirq.Pauli.X).commutes_with( + cirq.PauliString.from_single(q1, cirq.Pauli.Y)) + + assert cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y}).commutes_with( + cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y})) + assert not cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y} + ).commutes_with( + cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Z})) + assert cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y}).commutes_with( + cirq.PauliString({q0: cirq.Pauli.Y, q1: cirq.Pauli.X})) + assert cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y}).commutes_with( + cirq.PauliString({q0: cirq.Pauli.Y, q1: cirq.Pauli.Z})) + + assert cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y}).commutes_with( + cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y, + q2: cirq.Pauli.Z})) + assert not cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y} + ).commutes_with( + cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Z, + q2: cirq.Pauli.Z})) + assert cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y}).commutes_with( + cirq.PauliString({q0: cirq.Pauli.Y, q1: cirq.Pauli.X, + q2: cirq.Pauli.Z})) + assert cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y}).commutes_with( + cirq.PauliString({q0: cirq.Pauli.Y, q1: cirq.Pauli.Z, + q2: cirq.Pauli.X})) + + assert cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y}).commutes_with( + cirq.PauliString({q2: cirq.Pauli.X, q1: cirq.Pauli.Y})) + assert not cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y} + ).commutes_with( + cirq.PauliString({q2: cirq.Pauli.X, q1: cirq.Pauli.Z})) + assert not cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y} + ).commutes_with( + cirq.PauliString({q2: cirq.Pauli.Y, q1: cirq.Pauli.X})) + assert not cirq.PauliString({q0: cirq.Pauli.X, q1: cirq.Pauli.Y} + ).commutes_with( + cirq.PauliString({q2: cirq.Pauli.Y, q1: cirq.Pauli.Z})) def test_negate(): q0, q1 = _make_qubits(2) - qubit_pauli_map = {q0: Pauli.X, q1: Pauli.Y} - ps1 = PauliString(qubit_pauli_map) - ps2 = PauliString(qubit_pauli_map, True) + qubit_pauli_map = {q0: cirq.Pauli.X, q1: cirq.Pauli.Y} + ps1 = cirq.PauliString(qubit_pauli_map) + ps2 = cirq.PauliString(qubit_pauli_map, True) assert ps1.negate() == -ps1 == ps2 assert ps1 == ps2.negate() == -ps2 assert ps1.negate().negate() == ps1 @@ -242,19 +256,19 @@ def test_negate(): def test_pos(): q0, q1 = _make_qubits(2) - qubit_pauli_map = {q0: Pauli.X, q1: Pauli.Y} - ps1 = PauliString(qubit_pauli_map) + qubit_pauli_map = {q0: cirq.Pauli.X, q1: cirq.Pauli.Y} + ps1 = cirq.PauliString(qubit_pauli_map) assert ps1 == +ps1 def test_map_qubits(): a, b = (cirq.NamedQubit(name) for name in 'ab') q0, q1 = _make_qubits(2) - qubit_pauli_map1 = {a: Pauli.X, b: Pauli.Y} - qubit_pauli_map2 = {q0: Pauli.X, q1: Pauli.Y} + qubit_pauli_map1 = {a: cirq.Pauli.X, b: cirq.Pauli.Y} + qubit_pauli_map2 = {q0: cirq.Pauli.X, q1: cirq.Pauli.Y} qubit_map = {a: q0, b: q1} - ps1 = PauliString(qubit_pauli_map1) - ps2 = PauliString(qubit_pauli_map2) + ps1 = cirq.PauliString(qubit_pauli_map1) + ps2 = cirq.PauliString(qubit_pauli_map2) assert ps1.map_qubits(qubit_map) == ps2 @@ -268,30 +282,30 @@ def _assert_pass_over(ops, before, after): itertools.product(range(3), (True, False))) def test_pass_operations_over_single(shift, t_or_f): q0, q1 = _make_qubits(2) - X, Y, Z = (pauli+shift for pauli in Pauli.XYZ) + X, Y, Z = (pauli+shift for pauli in cirq.Pauli.XYZ) - op0 = CliffordGate.from_pauli(Y)(q1) - ps_before = PauliString({q0: X}, t_or_f) + op0 = cirq.CliffordGate.from_pauli(Y)(q1) + ps_before = cirq.PauliString({q0: X}, t_or_f) ps_after = ps_before _assert_pass_over([op0], ps_before, ps_after) - op0 = CliffordGate.from_pauli(X)(q0) - op1 = CliffordGate.from_pauli(Y)(q1) - ps_before = PauliString({q0: X, q1: Y}, t_or_f) + op0 = cirq.CliffordGate.from_pauli(X)(q0) + op1 = cirq.CliffordGate.from_pauli(Y)(q1) + ps_before = cirq.PauliString({q0: X, q1: Y}, t_or_f) ps_after = ps_before _assert_pass_over([op0, op1], ps_before, ps_after) - op0 = CliffordGate.from_double_map({Z: (X,False), X: (Z,False)})(q0) - ps_before = PauliString({q0: X, q1: Y}, t_or_f) - ps_after = PauliString({q0: Z, q1: Y}, t_or_f) + op0 = cirq.CliffordGate.from_double_map({Z: (X,False), X: (Z,False)})(q0) + ps_before = cirq.PauliString({q0: X, q1: Y}, t_or_f) + ps_after = cirq.PauliString({q0: Z, q1: Y}, t_or_f) _assert_pass_over([op0], ps_before, ps_after) - op1 = CliffordGate.from_pauli(X)(q1) - ps_before = PauliString({q0: X, q1: Y}, t_or_f) + op1 = cirq.CliffordGate.from_pauli(X)(q1) + ps_before = cirq.PauliString({q0: X, q1: Y}, t_or_f) ps_after = ps_before.negate() _assert_pass_over([op1], ps_before, ps_after) - ps_after = PauliString({q0: Z, q1: Y}, not t_or_f) + ps_after = cirq.PauliString({q0: Z, q1: Y}, not t_or_f) _assert_pass_over([op0, op1], ps_before, ps_after) @@ -299,36 +313,36 @@ def test_pass_operations_over_single(shift, t_or_f): itertools.product(range(3), *((True, False),)*3)) def test_pass_operations_over_double(shift, t_or_f1, t_or_f2, neg): q0, q1, q2 = _make_qubits(3) - X, Y, Z = (pauli+shift for pauli in Pauli.XYZ) + X, Y, Z = (pauli+shift for pauli in cirq.Pauli.XYZ) - op0 = PauliInteractionGate(Z, t_or_f1, X, t_or_f2)(q0, q1) - ps_before = PauliString({q0: Z, q2: Y}, neg) - ps_after = PauliString({q0: Z, q2: Y}, neg) + op0 = cirq.PauliInteractionGate(Z, t_or_f1, X, t_or_f2)(q0, q1) + ps_before = cirq.PauliString({q0: Z, q2: Y}, neg) + ps_after = cirq.PauliString({q0: Z, q2: Y}, neg) _assert_pass_over([op0], ps_before, ps_after) - op0 = PauliInteractionGate(Y, t_or_f1, X, t_or_f2)(q0, q1) - ps_before = PauliString({q0: Z, q2: Y}, neg) - ps_after = PauliString({q0: Z, q2: Y, q1: X}, neg) + op0 = cirq.PauliInteractionGate(Y, t_or_f1, X, t_or_f2)(q0, q1) + ps_before = cirq.PauliString({q0: Z, q2: Y}, neg) + ps_after = cirq.PauliString({q0: Z, q2: Y, q1: X}, neg) _assert_pass_over([op0], ps_before, ps_after) - op0 = PauliInteractionGate(Z, t_or_f1, X, t_or_f2)(q0, q1) - ps_before = PauliString({q0: Z, q1: Y}, neg) - ps_after = PauliString({q1: Y}, neg) + op0 = cirq.PauliInteractionGate(Z, t_or_f1, X, t_or_f2)(q0, q1) + ps_before = cirq.PauliString({q0: Z, q1: Y}, neg) + ps_after = cirq.PauliString({q1: Y}, neg) _assert_pass_over([op0], ps_before, ps_after) - op0 = PauliInteractionGate(Y, t_or_f1, X, t_or_f2)(q0, q1) - ps_before = PauliString({q0: Z, q1: Y}, neg) - ps_after = PauliString({q0: X, q1: Z}, neg ^ t_or_f1 ^ t_or_f2) + op0 = cirq.PauliInteractionGate(Y, t_or_f1, X, t_or_f2)(q0, q1) + ps_before = cirq.PauliString({q0: Z, q1: Y}, neg) + ps_after = cirq.PauliString({q0: X, q1: Z}, neg ^ t_or_f1 ^ t_or_f2) _assert_pass_over([op0], ps_before, ps_after) - op0 = PauliInteractionGate(X, t_or_f1, X, t_or_f2)(q0, q1) - ps_before = PauliString({q0: Z, q1: Y}, neg) - ps_after = PauliString({q0: Y, q1: Z}, not neg ^ t_or_f1 ^ t_or_f2) + op0 = cirq.PauliInteractionGate(X, t_or_f1, X, t_or_f2)(q0, q1) + ps_before = cirq.PauliString({q0: Z, q1: Y}, neg) + ps_after = cirq.PauliString({q0: Y, q1: Z}, not neg ^ t_or_f1 ^ t_or_f2) _assert_pass_over([op0], ps_before, ps_after) def test_pass_unsupported_operations_over(): q0, = _make_qubits(1) - pauli_string = PauliString({q0: Pauli.X}) + pauli_string = cirq.PauliString({q0: cirq.Pauli.X}) with pytest.raises(TypeError): pauli_string.pass_operations_over([cirq.X(q0)]) diff --git a/cirq/ops/pauli_test.py b/cirq/ops/pauli_test.py index fd5877fe94c..61be501969a 100644 --- a/cirq/ops/pauli_test.py +++ b/cirq/ops/pauli_test.py @@ -15,141 +15,139 @@ import pytest import cirq -from cirq.contrib.paulistring import Pauli - def test_equals(): eq = cirq.testing.EqualsTester() - eq.add_equality_group(Pauli.X) - eq.add_equality_group(Pauli.Y) - eq.add_equality_group(Pauli.Z) + eq.add_equality_group(cirq.Pauli.X) + eq.add_equality_group(cirq.Pauli.Y) + eq.add_equality_group(cirq.Pauli.Z) def test_singletons(): - assert Pauli.XYZ[0] == Pauli.X - assert Pauli.XYZ[1] == Pauli.Y - assert Pauli.XYZ[2] == Pauli.Z - assert len(Pauli.XYZ) == 3 + assert cirq.Pauli.XYZ[0] == cirq.Pauli.X + assert cirq.Pauli.XYZ[1] == cirq.Pauli.Y + assert cirq.Pauli.XYZ[2] == cirq.Pauli.Z + assert len(cirq.Pauli.XYZ) == 3 def test_difference(): - assert Pauli.X - Pauli.X == 0 - assert Pauli.X - Pauli.Y == -1 - assert Pauli.X - Pauli.Z == 1 - assert Pauli.Y - Pauli.X == 1 - assert Pauli.Y - Pauli.Y == 0 - assert Pauli.Y - Pauli.Z == -1 - assert Pauli.Z - Pauli.X == -1 - assert Pauli.Z - Pauli.Y == 1 - assert Pauli.Z - Pauli.Z == 0 + assert cirq.Pauli.X - cirq.Pauli.X == 0 + assert cirq.Pauli.X - cirq.Pauli.Y == -1 + assert cirq.Pauli.X - cirq.Pauli.Z == 1 + assert cirq.Pauli.Y - cirq.Pauli.X == 1 + assert cirq.Pauli.Y - cirq.Pauli.Y == 0 + assert cirq.Pauli.Y - cirq.Pauli.Z == -1 + assert cirq.Pauli.Z - cirq.Pauli.X == -1 + assert cirq.Pauli.Z - cirq.Pauli.Y == 1 + assert cirq.Pauli.Z - cirq.Pauli.Z == 0 def test_gt(): - assert not Pauli.X > Pauli.X - assert not Pauli.X > Pauli.Y - assert Pauli.X > Pauli.Z - assert Pauli.Y > Pauli.X - assert not Pauli.Y > Pauli.Y - assert not Pauli.Y > Pauli.Z - assert not Pauli.Z > Pauli.X - assert Pauli.Z > Pauli.Y - assert not Pauli.Z > Pauli.Z + assert not cirq.Pauli.X > cirq.Pauli.X + assert not cirq.Pauli.X > cirq.Pauli.Y + assert cirq.Pauli.X > cirq.Pauli.Z + assert cirq.Pauli.Y > cirq.Pauli.X + assert not cirq.Pauli.Y > cirq.Pauli.Y + assert not cirq.Pauli.Y > cirq.Pauli.Z + assert not cirq.Pauli.Z > cirq.Pauli.X + assert cirq.Pauli.Z > cirq.Pauli.Y + assert not cirq.Pauli.Z > cirq.Pauli.Z @cirq.testing.only_test_in_python3 def test_gt_other_type(): with pytest.raises(TypeError): - _ = Pauli.X > object() + _ = cirq.Pauli.X > object() def test_lt(): - assert not Pauli.X < Pauli.X - assert Pauli.X < Pauli.Y - assert not Pauli.X < Pauli.Z - assert not Pauli.Y < Pauli.X - assert not Pauli.Y < Pauli.Y - assert Pauli.Y < Pauli.Z - assert Pauli.Z < Pauli.X - assert not Pauli.Z < Pauli.Y - assert not Pauli.Z < Pauli.Z + assert not cirq.Pauli.X < cirq.Pauli.X + assert cirq.Pauli.X < cirq.Pauli.Y + assert not cirq.Pauli.X < cirq.Pauli.Z + assert not cirq.Pauli.Y < cirq.Pauli.X + assert not cirq.Pauli.Y < cirq.Pauli.Y + assert cirq.Pauli.Y < cirq.Pauli.Z + assert cirq.Pauli.Z < cirq.Pauli.X + assert not cirq.Pauli.Z < cirq.Pauli.Y + assert not cirq.Pauli.Z < cirq.Pauli.Z @cirq.testing.only_test_in_python3 def test_lt_other_type(): with pytest.raises(TypeError): - _ = Pauli.X < object() + _ = cirq.Pauli.X < object() def test_addition(): - assert Pauli.X + -3 == Pauli.X - assert Pauli.X + -2 == Pauli.Y - assert Pauli.X + -1 == Pauli.Z - assert Pauli.X + 0 == Pauli.X - assert Pauli.X + 1 == Pauli.Y - assert Pauli.X + 2 == Pauli.Z - assert Pauli.X + 3 == Pauli.X - assert Pauli.X + 4 == Pauli.Y - assert Pauli.X + 5 == Pauli.Z - assert Pauli.X + 6 == Pauli.X - assert Pauli.Y + 0 == Pauli.Y - assert Pauli.Y + 1 == Pauli.Z - assert Pauli.Y + 2 == Pauli.X - assert Pauli.Z + 0 == Pauli.Z - assert Pauli.Z + 1 == Pauli.X - assert Pauli.Z + 2 == Pauli.Y + assert cirq.Pauli.X + -3 == cirq.Pauli.X + assert cirq.Pauli.X + -2 == cirq.Pauli.Y + assert cirq.Pauli.X + -1 == cirq.Pauli.Z + assert cirq.Pauli.X + 0 == cirq.Pauli.X + assert cirq.Pauli.X + 1 == cirq.Pauli.Y + assert cirq.Pauli.X + 2 == cirq.Pauli.Z + assert cirq.Pauli.X + 3 == cirq.Pauli.X + assert cirq.Pauli.X + 4 == cirq.Pauli.Y + assert cirq.Pauli.X + 5 == cirq.Pauli.Z + assert cirq.Pauli.X + 6 == cirq.Pauli.X + assert cirq.Pauli.Y + 0 == cirq.Pauli.Y + assert cirq.Pauli.Y + 1 == cirq.Pauli.Z + assert cirq.Pauli.Y + 2 == cirq.Pauli.X + assert cirq.Pauli.Z + 0 == cirq.Pauli.Z + assert cirq.Pauli.Z + 1 == cirq.Pauli.X + assert cirq.Pauli.Z + 2 == cirq.Pauli.Y def test_subtraction(): - assert Pauli.X - -3 == Pauli.X - assert Pauli.X - -2 == Pauli.Z - assert Pauli.X - -1 == Pauli.Y - assert Pauli.X - 0 == Pauli.X - assert Pauli.X - 1 == Pauli.Z - assert Pauli.X - 2 == Pauli.Y - assert Pauli.X - 3 == Pauli.X - assert Pauli.X - 4 == Pauli.Z - assert Pauli.X - 5 == Pauli.Y - assert Pauli.X - 6 == Pauli.X - assert Pauli.Y - 0 == Pauli.Y - assert Pauli.Y - 1 == Pauli.X - assert Pauli.Y - 2 == Pauli.Z - assert Pauli.Z - 0 == Pauli.Z - assert Pauli.Z - 1 == Pauli.Y - assert Pauli.Z - 2 == Pauli.X + assert cirq.Pauli.X - -3 == cirq.Pauli.X + assert cirq.Pauli.X - -2 == cirq.Pauli.Z + assert cirq.Pauli.X - -1 == cirq.Pauli.Y + assert cirq.Pauli.X - 0 == cirq.Pauli.X + assert cirq.Pauli.X - 1 == cirq.Pauli.Z + assert cirq.Pauli.X - 2 == cirq.Pauli.Y + assert cirq.Pauli.X - 3 == cirq.Pauli.X + assert cirq.Pauli.X - 4 == cirq.Pauli.Z + assert cirq.Pauli.X - 5 == cirq.Pauli.Y + assert cirq.Pauli.X - 6 == cirq.Pauli.X + assert cirq.Pauli.Y - 0 == cirq.Pauli.Y + assert cirq.Pauli.Y - 1 == cirq.Pauli.X + assert cirq.Pauli.Y - 2 == cirq.Pauli.Z + assert cirq.Pauli.Z - 0 == cirq.Pauli.Z + assert cirq.Pauli.Z - 1 == cirq.Pauli.Y + assert cirq.Pauli.Z - 2 == cirq.Pauli.X def test_str(): - assert str(Pauli.X) == 'X' - assert str(Pauli.Y) == 'Y' - assert str(Pauli.Z) == 'Z' + assert str(cirq.Pauli.X) == 'X' + assert str(cirq.Pauli.Y) == 'Y' + assert str(cirq.Pauli.Z) == 'Z' def test_repr(): - assert repr(Pauli.X) == 'Pauli.X' - assert repr(Pauli.Y) == 'Pauli.Y' - assert repr(Pauli.Z) == 'Pauli.Z' + assert repr(cirq.Pauli.X) == 'Pauli.X' + assert repr(cirq.Pauli.Y) == 'Pauli.Y' + assert repr(cirq.Pauli.Z) == 'Pauli.Z' def test_third(): - assert Pauli.X.third(Pauli.Y) == Pauli.Z - assert Pauli.Y.third(Pauli.X) == Pauli.Z - assert Pauli.Y.third(Pauli.Z) == Pauli.X - assert Pauli.Z.third(Pauli.Y) == Pauli.X - assert Pauli.Z.third(Pauli.X) == Pauli.Y - assert Pauli.X.third(Pauli.Z) == Pauli.Y + assert cirq.Pauli.X.third(cirq.Pauli.Y) == cirq.Pauli.Z + assert cirq.Pauli.Y.third(cirq.Pauli.X) == cirq.Pauli.Z + assert cirq.Pauli.Y.third(cirq.Pauli.Z) == cirq.Pauli.X + assert cirq.Pauli.Z.third(cirq.Pauli.Y) == cirq.Pauli.X + assert cirq.Pauli.Z.third(cirq.Pauli.X) == cirq.Pauli.Y + assert cirq.Pauli.X.third(cirq.Pauli.Z) == cirq.Pauli.Y - assert Pauli.X.third(Pauli.X) == Pauli.X - assert Pauli.Y.third(Pauli.Y) == Pauli.Y - assert Pauli.Z.third(Pauli.Z) == Pauli.Z + assert cirq.Pauli.X.third(cirq.Pauli.X) == cirq.Pauli.X + assert cirq.Pauli.Y.third(cirq.Pauli.Y) == cirq.Pauli.Y + assert cirq.Pauli.Z.third(cirq.Pauli.Z) == cirq.Pauli.Z def test_commutes_with(): - assert Pauli.X.commutes_with(Pauli.X) - assert not Pauli.X.commutes_with(Pauli.Y) - assert not Pauli.X.commutes_with(Pauli.Z) - assert not Pauli.Y.commutes_with(Pauli.X) - assert Pauli.Y.commutes_with(Pauli.Y) - assert not Pauli.Y.commutes_with(Pauli.Z) - assert not Pauli.Z.commutes_with(Pauli.X) - assert not Pauli.Z.commutes_with(Pauli.Y) - assert Pauli.Z.commutes_with(Pauli.Z) + assert cirq.Pauli.X.commutes_with(cirq.Pauli.X) + assert not cirq.Pauli.X.commutes_with(cirq.Pauli.Y) + assert not cirq.Pauli.X.commutes_with(cirq.Pauli.Z) + assert not cirq.Pauli.Y.commutes_with(cirq.Pauli.X) + assert cirq.Pauli.Y.commutes_with(cirq.Pauli.Y) + assert not cirq.Pauli.Y.commutes_with(cirq.Pauli.Z) + assert not cirq.Pauli.Z.commutes_with(cirq.Pauli.X) + assert not cirq.Pauli.Z.commutes_with(cirq.Pauli.Y) + assert cirq.Pauli.Z.commutes_with(cirq.Pauli.Z)