def test_equal_up_to_global_numpy_array(): assert cirq.equal_up_to_global_phase( np.asarray([1j, 1j]), np.asarray([1, 1], dtype=np.complex64)) assert not cirq.equal_up_to_global_phase( np.asarray([1j, -1j]), np.asarray([1, 1], dtype=np.complex64)) assert cirq.equal_up_to_global_phase(np.asarray([]), np.asarray([])) assert cirq.equal_up_to_global_phase(np.asarray([[]]), np.asarray([[]]))
def test_equal_up_to_global_phase_on_gates(gate1, gate2, eq_up_to_global_phase): num_qubits1, num_qubits2 = (cirq.num_qubits(g) for g in (gate1, gate2)) qubits = cirq.LineQubit.range(max(num_qubits1, num_qubits2) + 1) op1, op2 = gate1(*qubits[:num_qubits1]), gate2(*qubits[:num_qubits2]) assert cirq.equal_up_to_global_phase(op1, op2) == eq_up_to_global_phase op2_on_diff_qubits = gate2(*qubits[1 : num_qubits2 + 1]) assert not cirq.equal_up_to_global_phase(op1, op2_on_diff_qubits)
def test_CNOT(): u1 = program_unitary(Program(CNOT(0, 1)), n_qubits=2) u2 = program_unitary(_CNOT(0, 1), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12) u1 = program_unitary(Program(CNOT(1, 0)), n_qubits=2) u2 = program_unitary(_CNOT(1, 0), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_CPHASE(): for theta in np.linspace(-2 * np.pi, 2 * np.pi): u1 = program_unitary(Program(CPHASE(theta, 0, 1)), n_qubits=2) u2 = program_unitary(_CPHASE(theta, 0, 1), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12) u1 = program_unitary(Program(CPHASE(theta, 1, 0)), n_qubits=2) u2 = program_unitary(_CPHASE(theta, 1, 0), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_XY(): for theta in np.linspace(-2 * np.pi, 2 * np.pi): p = Program(XY(theta, 0, 1)) u1 = program_unitary(p, n_qubits=2) u2 = program_unitary(basic_compile(p), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12) p = Program(XY(theta, 1, 0)) u1 = program_unitary(p, n_qubits=2) u2 = program_unitary(basic_compile(p), n_qubits=2) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_fsim_phase_corrections(theta: float, zeta: float, chi: float, gamma: float, phi: float) -> None: a, b = cirq.LineQubit.range(2) expected_gate = cirq.PhasedFSimGate(theta=theta, zeta=-zeta, chi=-chi, gamma=-gamma, phi=phi) expected = cirq.unitary(expected_gate) corrected = workflow.FSimPhaseCorrections.from_characterization( (a, b), cirq.FSimGate(theta=theta, phi=phi), cirq.google.PhasedFSimCharacterization(theta=theta, zeta=zeta, chi=chi, gamma=gamma, phi=phi), 5, ) actual = cirq.unitary(corrected.as_circuit()) assert cirq.equal_up_to_global_phase(actual, expected) assert corrected.moment_to_calibration == [None, 5, None]
def test_phase_corrected_fsim_operations_with_phase_exponent( theta: float, zeta: float, chi: float, gamma: float, phi: float) -> None: a, b = cirq.LineQubit.range(2) phase_exponent = 0.5 # Theta is negated to match the phase exponent of 0.5. expected_gate = cirq.PhasedFSimGate(theta=-theta, zeta=-zeta, chi=-chi, gamma=-gamma, phi=phi) expected = cirq.unitary(expected_gate) corrected = workflow.FSimPhaseCorrections.from_characterization( (a, b), PhaseCalibratedFSimGate(cirq.FSimGate(theta=theta, phi=phi), phase_exponent), cirq_google.PhasedFSimCharacterization(theta=theta, zeta=zeta, chi=chi, gamma=gamma, phi=phi), characterization_index=5, ) actual = cirq.unitary(corrected.as_circuit()) assert cirq.equal_up_to_global_phase(actual, expected) assert corrected.moment_to_calibration == [None, 5, None]
def mixtures_equal(m1, m2, atol=1e-7): for (p1, v1), (p2, v2) in zip(m1, m2): if not ( cirq.approx_eq(p1, p2, atol=atol) and cirq.equal_up_to_global_phase(v1, v2, atol=atol) ): return False return True
def test_equal_up_to_global_phase_eq_supported(): assert cirq.equal_up_to_global_phase(A(0.1 + 0j), A(0.1j), atol=1e-2) assert not cirq.equal_up_to_global_phase(A(0.0 + 0j), A(0.1j), atol=0.0) assert not cirq.equal_up_to_global_phase(A(0.0 + 0j), 0.1j, atol=0.0) assert cirq.equal_up_to_global_phase(B(0.0j), 1e-8j, atol=1e-8) assert cirq.equal_up_to_global_phase(1e-8j, B(0.0j), atol=1e-8) assert not cirq.equal_up_to_global_phase(1e-8j, B(0.0 + 0j), atol=1e-10) # cast types assert cirq.equal_up_to_global_phase(A(0.1), A(0.1j), atol=1e-2) assert not cirq.equal_up_to_global_phase(1e-8j, B(0.0), atol=1e-10)
def test_random_circuit_to_from_circuits(): """Tests cirq.Circuit --> qiskit.QuantumCircuit --> cirq.Circuit with a random two-qubit circuit. """ cirq_circuit = cirq.testing.random_circuit( qubits=2, n_moments=10, op_density=0.99, random_state=1 ) qiskit_circuit = to_qiskit(cirq_circuit) circuit_cirq = from_qiskit(qiskit_circuit) assert cirq.equal_up_to_global_phase( cirq_circuit.unitary(), circuit_cirq.unitary() )
def test_random_circuit_to_from_qasm(): """Tests cirq.Circuit --> QASM string --> cirq.Circuit with a random one-qubit circuit. """ cirq_circuit = cirq.testing.random_circuit( qubits=2, n_moments=10, op_density=0.99, random_state=2 ) qasm = to_qasm(cirq_circuit) circuit_cirq = from_qasm(qasm) assert cirq.equal_up_to_global_phase( cirq_circuit.unitary(), circuit_cirq.unitary() )
def test_sub_state_vector(): a = np.arange(4) / np.linalg.norm(np.arange(4)) b = (np.arange(8) + 3) / np.linalg.norm(np.arange(8) + 3) c = (np.arange(16) + 1) / np.linalg.norm(np.arange(16) + 1) state = np.kron(np.kron(a, b), c).reshape(2, 2, 2, 2, 2, 2, 2, 2, 2) assert cirq.equal_up_to_global_phase( cirq.sub_state_vector(a, [0, 1], atol=1e-8), a) assert cirq.equal_up_to_global_phase( cirq.sub_state_vector(b, [0, 1, 2], atol=1e-8), b) assert cirq.equal_up_to_global_phase( cirq.sub_state_vector(c, [0, 1, 2, 3], atol=1e-8), c) assert cirq.equal_up_to_global_phase( cirq.sub_state_vector(state, [0, 1], atol=1e-15), a.reshape(2, 2)) assert cirq.equal_up_to_global_phase( cirq.sub_state_vector(state, [2, 3, 4], atol=1e-15), b.reshape(2, 2, 2)) assert cirq.equal_up_to_global_phase( cirq.sub_state_vector(state, [5, 6, 7, 8], atol=1e-15), c.reshape(2, 2, 2, 2)) # Output state vector conforms to the shape of the input state vector. reshaped_state = state.reshape(-1) assert cirq.equal_up_to_global_phase( cirq.sub_state_vector(reshaped_state, [0, 1], atol=1e-15), a) assert cirq.equal_up_to_global_phase( cirq.sub_state_vector(reshaped_state, [2, 3, 4], atol=1e-15), b) assert cirq.equal_up_to_global_phase( cirq.sub_state_vector(reshaped_state, [5, 6, 7, 8], atol=1e-15), c) # Reject factoring for very tight tolerance. assert cirq.sub_state_vector(state, [0, 1], default=None, atol=1e-16) is None assert cirq.sub_state_vector(state, [2, 3, 4], default=None, atol=1e-16) is None assert cirq.sub_state_vector(state, [5, 6, 7, 8], default=None, atol=1e-16) is None # Permit invalid factoring for loose tolerance. for q1 in range(9): assert cirq.sub_state_vector(state, [q1], default=None, atol=1) is not None
def test_sub_state_vector_non_kron(): a = np.array([1, 0, 0, 0, 0, 0, 0, 1]) / np.sqrt(2) b = np.array([1, 1]) / np.sqrt(2) state = np.kron(a, b).reshape((2, 2, 2, 2)) for q1 in [0, 1, 2]: assert cirq.sub_state_vector(state, [q1], default=None, atol=1e-8) is None for q1 in [0, 1, 2]: assert cirq.sub_state_vector(state, [q1, 3], default=None, atol=1e-8) is None with pytest.raises(ValueError, match='factored'): _ = cirq.sub_state_vector(a, [0], atol=1e-8) assert cirq.equal_up_to_global_phase(cirq.sub_state_vector(state, [3]), b, atol=1e-8)
def test_corrected_sqrt_iswap_ops(delta: float, chi: float, gamma: float) -> None: a, b = cirq.LineQubit.range(2) matrix = cirq.unitary( cirq.Circuit( _corrected_sqrt_iswap_ops(qubits=(a, b), parameters=ParticleConservingParameters( delta=delta, chi=chi, gamma=gamma)))) expected = create_particle_conserving_matrix(np.pi / 4, -delta, -chi, -gamma, 0) assert cirq.equal_up_to_global_phase(matrix, expected)
def test_corr_hop_gate(theta: float) -> None: a, b = cirq.LineQubit.range(2) matrix = cirq.unitary( cirq.Circuit( _corrected_gk_ops(qubits=(a, b), angle=theta, phase_exponent=0.0, parameters=ParticleConservingParameters( theta=theta, delta=0, chi=0, gamma=0)))) expected = create_particle_conserving_matrix(theta, 0, 0, 0, 0) assert cirq.equal_up_to_global_phase(matrix, expected)
def test_corrected_cphase_ops(cphase: float) -> None: a, b = cirq.LineQubit.range(2) matrix = cirq.unitary( cirq.Circuit( _corrected_cphase_ops(qubits=(a, b), angle=cphase, parameters=ParticleConservingParameters( theta=np.pi / 4, delta=0, chi=0, gamma=0, phi=0)))) expected = create_particle_conserving_matrix(0, 0, 0, 0, cphase) assert cirq.equal_up_to_global_phase(matrix, expected)
def test_equal_up_to_global_phase(gate1, gate2, eq_up_to_global_phase): assert cirq.equal_up_to_global_phase(gate1, gate2) == eq_up_to_global_phase
def _equal_up_to_global_phase_(self, other, atol): if not isinstance(self.val[0], type(other)): return NotImplemented return cirq.equal_up_to_global_phase(self.val[0], other, atol=atol)
def is_pauli(u): return any(cirq.equal_up_to_global_phase(u, p) for p in PAULIS)
def test_equal_up_to_global_phase_primitives(): assert cirq.equal_up_to_global_phase(1.0 + 1j, 1.0 + 1j, atol=1e-9) assert not cirq.equal_up_to_global_phase(2.0, 1.0 + 1j, atol=1e-9) assert cirq.equal_up_to_global_phase(1.0 + 1j, 1.0 - 1j, atol=1e-9) assert cirq.equal_up_to_global_phase(np.exp(1j * 3.3), 1.0 + 0.0j, atol=1e-9) assert cirq.equal_up_to_global_phase(np.exp(1j * 3.3), 1.0j, atol=1e-9) assert cirq.equal_up_to_global_phase(np.exp(1j * 3.3), 1, atol=1e-9) assert not cirq.equal_up_to_global_phase(np.exp(1j * 3.3), 0, atol=1e-9) assert cirq.equal_up_to_global_phase(1j, 1 + 1e-10, atol=1e-9) assert not cirq.equal_up_to_global_phase(1j, 1 + 1e-10, atol=1e-11) # atol is applied to magnitude of complex vector, not components. assert cirq.equal_up_to_global_phase(1.0 + 0.1j, 1.0, atol=0.01) assert not cirq.equal_up_to_global_phase(1.0 + 0.1j, 1.0, atol=0.001) assert cirq.equal_up_to_global_phase(1.0 + 1j, np.sqrt(2) + 1e-8, atol=1e-7) assert not cirq.equal_up_to_global_phase( 1.0 + 1j, np.sqrt(2) + 1e-7, atol=1e-8) assert cirq.equal_up_to_global_phase(1.0 + 1e-10j, 1.0, atol=1e-15)
def test_equal_up_to_global_numeric_iterables(): assert cirq.equal_up_to_global_phase([], [], atol=1e-9) assert cirq.equal_up_to_global_phase([[]], [[]], atol=1e-9) assert cirq.equal_up_to_global_phase([1j, 1], [1j, 1], atol=1e-9) assert cirq.equal_up_to_global_phase([1j, 1j], [1 + 0.1j, 1 + 0.1j], atol=0.01) assert not cirq.equal_up_to_global_phase([1j, 1j], [1 + 0.1j, 1 - 0.1j], atol=0.01) assert not cirq.equal_up_to_global_phase([1j, 1j], [1 + 0.1j, 1 + 0.1j], atol=1e-3) assert not cirq.equal_up_to_global_phase([1j, -1j], [1, 1], atol=0.0) assert not cirq.equal_up_to_global_phase([1j, 1], [1, 1j], atol=0.0) assert not cirq.equal_up_to_global_phase([1j, 1], [1j, 1, 0], atol=0.0) assert cirq.equal_up_to_global_phase((1j, 1j), (1, 1 + 1e-4), atol=1e-3) assert not cirq.equal_up_to_global_phase( (1j, 1j), (1, 1 + 1e-4), atol=1e-5) assert not cirq.equal_up_to_global_phase((1j, 1), (1, 1j), atol=1e-09)
def test_equal_up_to_global_mixed_array_types(): a = [1j, 1, -1j, -1] b = [-1, 1j, 1, -1j] c = [-1, 1, -1, 1] assert cirq.equal_up_to_global_phase(a, tuple(b)) assert not cirq.equal_up_to_global_phase(a, tuple(c)) c_types = [np.complex64, np.complex128] if hasattr(np, 'complex256'): c_types.append(np.complex256) for c_type in c_types: assert cirq.equal_up_to_global_phase(np.asarray(a, dtype=c_type), tuple(b)) assert not cirq.equal_up_to_global_phase(np.asarray(a, dtype=c_type), tuple(c)) assert cirq.equal_up_to_global_phase(np.asarray(a, dtype=c_type), b) assert not cirq.equal_up_to_global_phase(np.asarray(a, dtype=c_type), c) # Object arrays and mixed array/scalar comparisons. assert not cirq.equal_up_to_global_phase([1j], 1j) assert not cirq.equal_up_to_global_phase( np.asarray([1], dtype=np.complex128), np.exp(1j)) assert not cirq.equal_up_to_global_phase([1j, 1j], [1j, "1j"]) assert not cirq.equal_up_to_global_phase([1j], "Non-numeric iterable") assert not cirq.equal_up_to_global_phase([], [[]], atol=0.0)
def test_CCNOT(): for perm in itertools.permutations([0, 1, 2]): u1 = program_unitary(Program(CCNOT(*perm)), n_qubits=3) u2 = program_unitary(_CCNOT(*perm), n_qubits=3) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_RY(): for theta in np.linspace(-2 * np.pi, 2 * np.pi): u1 = program_unitary(Program(RY(theta, 0)), n_qubits=1) u2 = program_unitary(_RY(theta, 0), n_qubits=1) assert equal_up_to_global_phase(u1, u2)
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_Z(): u1 = program_unitary(Program(Z(0)), n_qubits=1) u2 = program_unitary(_Z(0), n_qubits=1) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_random_progs(n_qubits, prog_length): for repeat_i in range(10): prog = _generate_random_program(n_qubits=n_qubits, length=prog_length) u1 = program_unitary(prog, n_qubits=n_qubits) u2 = program_unitary(basic_compile(prog), n_qubits=n_qubits) assert equal_up_to_global_phase(u1, u2, atol=1e-12)
def test_equal_up_to_global_phase_on_diff_types(): op = cirq.X(cirq.LineQubit(0)) assert not cirq.equal_up_to_global_phase(op, 3)
def test_all_single_qubit_clifford_unitaries(): i = np.eye(2) x = np.array([[0, 1], [1, 0]]) y = np.array([[0, -1j], [1j, 0]]) z = np.diag([1, -1]) cs = [cirq.unitary(c) for c in cirq.CliffordGate.all_single_qubit_cliffords] # Identity assert cirq.equal_up_to_global_phase(cs[0], i) # Paulis assert cirq.equal_up_to_global_phase(cs[1], x) assert cirq.equal_up_to_global_phase(cs[2], y) assert cirq.equal_up_to_global_phase(cs[3], z) # Square roots of Paulis assert cirq.equal_up_to_global_phase(cs[4], (i - 1j * x) / np.sqrt(2)) assert cirq.equal_up_to_global_phase(cs[5], (i - 1j * y) / np.sqrt(2)) assert cirq.equal_up_to_global_phase(cs[6], (i - 1j * z) / np.sqrt(2)) # Negative square roots of Paulis assert cirq.equal_up_to_global_phase(cs[7], (i + 1j * x) / np.sqrt(2)) assert cirq.equal_up_to_global_phase(cs[8], (i + 1j * y) / np.sqrt(2)) assert cirq.equal_up_to_global_phase(cs[9], (i + 1j * z) / np.sqrt(2)) # Hadamards assert cirq.equal_up_to_global_phase(cs[10], (z + x) / np.sqrt(2)) assert cirq.equal_up_to_global_phase(cs[11], (x + y) / np.sqrt(2)) assert cirq.equal_up_to_global_phase(cs[12], (y + z) / np.sqrt(2)) assert cirq.equal_up_to_global_phase(cs[13], (z - x) / np.sqrt(2)) assert cirq.equal_up_to_global_phase(cs[14], (x - y) / np.sqrt(2)) assert cirq.equal_up_to_global_phase(cs[15], (y - z) / np.sqrt(2)) # Order-3 Cliffords assert cirq.equal_up_to_global_phase(cs[16], (i - 1j * (x + y + z)) / 2) assert cirq.equal_up_to_global_phase(cs[17], (i - 1j * (x + y - z)) / 2) assert cirq.equal_up_to_global_phase(cs[18], (i - 1j * (x - y + z)) / 2) assert cirq.equal_up_to_global_phase(cs[19], (i - 1j * (x - y - z)) / 2) assert cirq.equal_up_to_global_phase(cs[20], (i - 1j * (-x + y + z)) / 2) assert cirq.equal_up_to_global_phase(cs[21], (i - 1j * (-x + y - z)) / 2) assert cirq.equal_up_to_global_phase(cs[22], (i - 1j * (-x - y + z)) / 2) assert cirq.equal_up_to_global_phase(cs[23], (i - 1j * (-x - y - z)) / 2)