def test_measurement_gate_diagram(): # Shows observable & key. assert cirq.circuit_diagram_info( cirq.PauliMeasurementGate([cirq.X], key='test') ) == cirq.CircuitDiagramInfo(("M(X)('test')",)) # Shows multiple observables. assert cirq.circuit_diagram_info( cirq.PauliMeasurementGate([cirq.X, cirq.Y, cirq.Z], 'a') ) == cirq.CircuitDiagramInfo(("M(X)('a')", 'M(Y)', 'M(Z)')) # Omits key when it is the default. a = cirq.NamedQubit('a') b = cirq.NamedQubit('b') cirq.testing.assert_has_diagram( cirq.Circuit(cirq.measure_single_paulistring(cirq.X(a) * cirq.Y(b))), """ a: ───M(X)─── │ b: ───M(Y)─── """, ) cirq.testing.assert_has_diagram( cirq.Circuit(cirq.measure_single_paulistring(cirq.X(a) * cirq.Y(b), key='test')), """ a: ───M(X)('test')─── │ b: ───M(Y)─────────── """, )
def test_with_observable(): o1 = [cirq.Z, cirq.Y, cirq.X] o2 = [cirq.X, cirq.Y, cirq.Z] g1 = cirq.PauliMeasurementGate(o1, key='a') g2 = cirq.PauliMeasurementGate(o2, key='a') assert g1.with_observable(o2) == g2 assert g1.with_observable(o1) is g1
def test_measurement_eq(): eq = cirq.testing.EqualsTester() eq.make_equality_group( lambda: cirq.PauliMeasurementGate([cirq.X, cirq.Y], 'a'), lambda: cirq.PauliMeasurementGate([cirq.X, cirq.Y], cirq.MeasurementKey('a')), ) eq.add_equality_group(cirq.PauliMeasurementGate([cirq.X, cirq.Y], 'b')) eq.add_equality_group(cirq.PauliMeasurementGate([cirq.Y, cirq.X], 'a'))
def test_bad_observable_raises(): with pytest.raises(ValueError, match='Pauli observable .* is empty'): _ = cirq.PauliMeasurementGate([]) with pytest.raises(ValueError, match=r'Pauli observable .* must be Iterable\[`cirq.Pauli`\]'): _ = cirq.PauliMeasurementGate([cirq.I, cirq.X, cirq.Y]) with pytest.raises(ValueError, match=r'Pauli observable .* must be Iterable\[`cirq.Pauli`\]'): _ = cirq.PauliMeasurementGate(cirq.DensePauliString('XYZI'))
def test_init(observable, key): g = cirq.PauliMeasurementGate(observable, key) assert g.num_qubits() == len(observable) assert g.key == 'a' assert g.mkey == cirq.MeasurementKey('a') assert g._observable == cirq.DensePauliString(observable) assert cirq.qid_shape(g) == (2,) * len(observable)
def test_measure_paulistring_terms(): # Correct application q = cirq.LineQubit.range(3) ps = cirq.X(q[0]) * cirq.Y(q[1]) * cirq.Z(q[2]) assert cirq.measure_paulistring_terms(ps) == [ cirq.PauliMeasurementGate([cirq.X], key=str(q[0])).on(q[0]), cirq.PauliMeasurementGate([cirq.Y], key=str(q[1])).on(q[1]), cirq.PauliMeasurementGate([cirq.Z], key=str(q[2])).on(q[2]), ] # Empty application with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'): _ = cirq.measure_paulistring_terms(cirq.I(q[0]) * cirq.I(q[1])) # Wrong type with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'): _ = cirq.measure_paulistring_terms(q)
def test_eval_repr(key): # Basic safeguard against repr-inequality. op = cirq.GateOperation( gate=cirq.PauliMeasurementGate([cirq.X, cirq.Y], key), qubits=[cirq.GridQubit(0, 1), cirq.GridQubit(1, 1)], ) cirq.testing.assert_equivalent_repr(op) assert cirq.is_measurement(op) assert cirq.measurement_key_name(op) == str(key)
def test_setters_deprecated(): gate = cirq.PauliMeasurementGate(cirq.DensePauliString("Z", coefficient=+1), key='m') with cirq.testing.assert_deprecated('mutators', deadline='v0.15'): gate.key = 'n' assert gate.key == 'n' assert gate.mkey == cirq.MeasurementKey('n') with cirq.testing.assert_deprecated('mutators', deadline='v0.15'): gate.key = cirq.MeasurementKey('o') assert gate.key == 'o' assert gate.mkey == cirq.MeasurementKey('o') with cirq.testing.assert_deprecated('mutators', deadline='v0.15'): gate.mkey = cirq.MeasurementKey('p') assert gate.key == 'p' assert gate.mkey == cirq.MeasurementKey('p')
def test_measure_single_paulistring(): # Correct application q = cirq.LineQubit.range(3) ps = cirq.X(q[0]) * cirq.Y(q[1]) * cirq.Z(q[2]) assert cirq.measure_single_paulistring(ps, key='a') == cirq.PauliMeasurementGate( ps.values(), key='a' ).on(*ps.keys()) # Empty application with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'): _ = cirq.measure_single_paulistring(cirq.I(q[0]) * cirq.I(q[1])) # Wrong type with pytest.raises(ValueError, match='should be an instance of cirq.PauliString'): _ = cirq.measure_single_paulistring(q)
def test_pauli(): q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit( cirq.PauliMeasurementGate(cirq.DensePauliString('Y'), key='a').on(q0), cirq.X(q1).with_classical_controls('a'), cirq.measure(q1, key='b'), ) deferred = cirq.defer_measurements(circuit) q_ma = _MeasurementQid('a', q0) cirq.testing.assert_same_circuits( cirq.unroll_circuit_op(deferred), cirq.Circuit( cirq.SingleQubitCliffordGate.X_sqrt(q0), cirq.CX(q0, q_ma), (cirq.SingleQubitCliffordGate.X_sqrt(q0)**-1), cirq.Moment(cirq.CX(q_ma, q1)), cirq.measure(q_ma, key='a'), cirq.measure(q1, key='b'), ), )
def test_measurement_has_unitary_returns_false(): gate = cirq.PauliMeasurementGate([cirq.X], 'a') assert not cirq.has_unitary(gate)
def test_pauli_measurement_gate_samples(rot, obs, out): q = cirq.NamedQubit("q") c = cirq.Circuit(rot(q), cirq.PauliMeasurementGate(obs, key='out').on(q)) assert cirq.Simulator().sample(c)['out'][0] == out
def test_consistent_protocols(observable, key): gate = cirq.PauliMeasurementGate(observable, key=key) cirq.testing.assert_implements_consistent_protocols(gate) assert cirq.is_measurement(gate) assert cirq.measurement_key_name(gate) == str(key)
eq.add_equality_group(cirq.PauliMeasurementGate([cirq.X, cirq.Y], 'b')) eq.add_equality_group(cirq.PauliMeasurementGate([cirq.Y, cirq.X], 'a')) @pytest.mark.parametrize( 'protocol,args,key', [ (None, None, 'b'), (cirq.with_key_path, ('p', 'q'), 'p:q:a'), (cirq.with_measurement_key_mapping, {'a': 'b'}, 'b'), ], ) @pytest.mark.parametrize( 'gate', [ cirq.PauliMeasurementGate([cirq.X], 'a'), cirq.PauliMeasurementGate([cirq.X, cirq.Y, cirq.Z], 'a'), ], ) def test_measurement_with_key(protocol, args, key, gate): if protocol: gate_with_key = protocol(gate, args) else: gate_with_key = gate.with_key('b') assert gate_with_key.key == key assert gate_with_key.num_qubits() == gate.num_qubits() assert gate_with_key.observable() == gate.observable() assert cirq.qid_shape(gate_with_key) == cirq.qid_shape(gate) if protocol: same_gate = cirq.with_measurement_key_mapping(gate, {'a': 'a'}) else:
def test_inconclusive(): class No: pass assert not cirq.has_unitary(object()) assert not cirq.has_unitary('boo') assert not cirq.has_unitary(No()) @pytest.mark.parametrize( 'measurement_gate', ( cirq.MeasurementGate(1, 'a'), cirq.PauliMeasurementGate([cirq.X], 'a'), ), ) def test_fail_fast_measure(measurement_gate): assert not cirq.has_unitary(measurement_gate) qubit = cirq.NamedQubit('q0') circuit = cirq.Circuit() circuit += measurement_gate(qubit) circuit += cirq.H(qubit) assert not cirq.has_unitary(circuit) def test_fail_fast_measure_large_memory(): num_qubits = 100 measurement_op = cirq.MeasurementGate(
import cirq def test_inconclusive(): class No: pass assert not cirq.has_unitary(object()) assert not cirq.has_unitary('boo') assert not cirq.has_unitary(No()) @pytest.mark.parametrize( 'measurement_gate', (cirq.MeasurementGate(1, 'a'), cirq.PauliMeasurementGate([cirq.X], 'a'))) def test_fail_fast_measure(measurement_gate): assert not cirq.has_unitary(measurement_gate) qubit = cirq.NamedQubit('q0') circuit = cirq.Circuit() circuit += measurement_gate(qubit) circuit += cirq.H(qubit) assert not cirq.has_unitary(circuit) def test_fail_fast_measure_large_memory(): num_qubits = 100 measurement_op = cirq.MeasurementGate( num_qubits, 'a').on(*cirq.LineQubit.range(num_qubits)) assert not cirq.has_unitary(measurement_op)