def test_qasm(): assert cirq.qasm(NoMethod(), default=None) is None assert cirq.qasm(NoMethod(), default=5) == 5 assert cirq.qasm(ReturnsText()) == 'text' with pytest.raises(TypeError, match='no _qasm_ method'): _ = cirq.qasm(NoMethod()) with pytest.raises(TypeError, match='returned NotImplemented or None'): _ = cirq.qasm(ReturnsNotImplemented()) assert cirq.qasm(ExpectsArgs(), args=cirq.QasmArgs()) == 'text' assert cirq.qasm(ExpectsArgsQubits(), args=cirq.QasmArgs(), qubits=()) == 'text'
def test_qasm_output_args_format(): a = cirq.NamedQubit('a') b = cirq.NamedQubit('b') m_a = cirq.measure(a, key='meas_a') m_b = cirq.measure(b, key='meas_b') args = cirq.QasmArgs(precision=4, version='2.0', qubit_id_map={ a: 'aaa[0]', b: 'bbb[0]' }, meas_key_id_map={ 'meas_a': 'm_a', 'meas_b': 'm_b' }) assert args.format('_{0}_', a) == '_aaa[0]_' assert args.format('_{0}_', b) == '_bbb[0]_' assert args.format('_{0:meas}_', cirq.measurement_key(m_a)) == '_m_a_' assert args.format('_{0:meas}_', cirq.measurement_key(m_b)) == '_m_b_' assert args.format('_{0}_', 89.1234567) == '_89.1235_' assert args.format('_{0}_', 1.23) == '_1.23_' assert args.format('_{0:half_turns}_', 89.1234567) == '_pi*89.1235_' assert args.format('_{0:half_turns}_', 1.23) == '_pi*1.23_' assert args.format('_{0}_', 'other') == '_other_'
def test_confused_measure_qasm(): q0 = cirq.LineQubit(0) assert (cirq.qasm( cirq.measure(q0, key='a', confusion_map={(0, ): np.array([[0, 1], [1, 0]])}), args=cirq.QasmArgs(), default='not implemented', ) == 'not implemented')
def test_qudit_measure_qasm(): assert ( cirq.qasm( cirq.measure(cirq.LineQid(0, 3), key='a'), args=cirq.QasmArgs(), default='not implemented', ) == 'not implemented' )
def test_no_symbolic_qasm_but_fails_gracefully(): q = cirq.NamedQubit('q') v = cirq.PhasedXPowGate(phase_exponent=sympy.Symbol('p')).on(q) assert cirq.qasm(v, args=cirq.QasmArgs(), default=None) is None
def test_tagged_operation_forwards_protocols(): """The results of all protocols applied to an operation with a tag should be equivalent to the result without tags. """ q1 = cirq.GridQubit(1, 1) q2 = cirq.GridQubit(1, 2) h = cirq.H(q1) tag = 'tag1' tagged_h = cirq.H(q1).with_tags(tag) np.testing.assert_equal(cirq.unitary(tagged_h), cirq.unitary(h)) assert cirq.has_unitary(tagged_h) assert cirq.decompose(tagged_h) == cirq.decompose(h) assert cirq.pauli_expansion(tagged_h) == cirq.pauli_expansion(h) assert cirq.equal_up_to_global_phase(h, tagged_h) assert np.isclose(cirq.channel(h), cirq.channel(tagged_h)).all() assert cirq.measurement_key(cirq.measure(q1, key='blah').with_tags(tag)) == 'blah' parameterized_op = cirq.XPowGate(exponent=sympy.Symbol('t'))(q1).with_tags(tag) assert cirq.is_parameterized(parameterized_op) resolver = cirq.study.ParamResolver({'t': 0.25}) assert cirq.resolve_parameters(parameterized_op, resolver) == cirq.XPowGate(exponent=0.25)( q1 ).with_tags(tag) assert cirq.resolve_parameters_once(parameterized_op, resolver) == cirq.XPowGate(exponent=0.25)( q1 ).with_tags(tag) y = cirq.Y(q1) tagged_y = cirq.Y(q1).with_tags(tag) assert tagged_y ** 0.5 == cirq.YPowGate(exponent=0.5)(q1) assert tagged_y * 2 == (y * 2) assert 3 * tagged_y == (3 * y) assert cirq.phase_by(y, 0.125, 0) == cirq.phase_by(tagged_y, 0.125, 0) controlled_y = tagged_y.controlled_by(q2) assert controlled_y.qubits == ( q2, q1, ) assert isinstance(controlled_y, cirq.Operation) assert not isinstance(controlled_y, cirq.TaggedOperation) clifford_x = cirq.SingleQubitCliffordGate.X(q1) tagged_x = cirq.SingleQubitCliffordGate.X(q1).with_tags(tag) assert cirq.commutes(clifford_x, clifford_x) assert cirq.commutes(tagged_x, clifford_x) assert cirq.commutes(clifford_x, tagged_x) assert cirq.commutes(tagged_x, tagged_x) assert cirq.trace_distance_bound(y ** 0.001) == cirq.trace_distance_bound( (y ** 0.001).with_tags(tag) ) flip = cirq.bit_flip(0.5)(q1) tagged_flip = cirq.bit_flip(0.5)(q1).with_tags(tag) assert cirq.has_mixture(tagged_flip) assert cirq.has_channel(tagged_flip) flip_mixture = cirq.mixture(flip) tagged_mixture = cirq.mixture(tagged_flip) assert len(tagged_mixture) == 2 assert len(tagged_mixture[0]) == 2 assert len(tagged_mixture[1]) == 2 assert tagged_mixture[0][0] == flip_mixture[0][0] assert np.isclose(tagged_mixture[0][1], flip_mixture[0][1]).all() assert tagged_mixture[1][0] == flip_mixture[1][0] assert np.isclose(tagged_mixture[1][1], flip_mixture[1][1]).all() qubit_map = {q1: 'q1'} qasm_args = cirq.QasmArgs(qubit_id_map=qubit_map) assert cirq.qasm(h, args=qasm_args) == cirq.qasm(tagged_h, args=qasm_args) cirq.testing.assert_has_consistent_apply_unitary(tagged_h)
def test_qasm_output_args_validate(): args = cirq.QasmArgs(version='2.0') args.validate_version('2.0') with pytest.raises(ValueError): args.validate_version('2.1')