def test_deprecated_givens_rotation(angle_rads): with capture_logging() as log: u = cirq.unitary(cirq.GivensRotation(angle_rads)) assert len(log) == 1 assert 'deprecated' in log[0].getMessage() assert 'GivensRotation' in log[0].getMessage() np.testing.assert_allclose(u, cirq.unitary(cirq.givens(angle_rads)))
def test_givens_rotation(): """Test if the sqrt_iswap synthesis for a givens rotation is correct""" thetas = np.linspace(0, 2 * np.pi, 100) qubits = [cirq.NamedQubit('a'), cirq.NamedQubit('b')] for theta in thetas: program = cirq.Circuit(cirq.givens(theta).on(qubits[0], qubits[1])) unitary = cirq.unitary(program) test_program = program.copy() with cirq.testing.assert_deprecated( "Use cirq.optimize_for_target_gateset", deadline='v1.0'): cgoc.ConvertToSqrtIswapGates().optimize_circuit(test_program) converted_circuit_iswap_inv = cirq.optimize_for_target_gateset( test_program, gateset=cirq.SqrtIswapTargetGateset(use_sqrt_iswap_inv=True)) converted_circuit_iswap = cirq.optimize_for_target_gateset( test_program, gateset=cirq.SqrtIswapTargetGateset()) for circuit in [ test_program, converted_circuit_iswap_inv, converted_circuit_iswap ]: circuit.append(cirq.IdentityGate(2).on(*qubits)) test_unitary = cirq.unitary(circuit) np.testing.assert_allclose( 4, np.abs( np.trace( np.conjugate(np.transpose(test_unitary)) @ unitary)))
def test_givens_rotation_hamiltonian(angle_rads): actual = cirq.unitary(cirq.givens(angle_rads)) x = np.array([[0, 1], [1, 0]]) y = np.array([[0, -1j], [1j, 0]]) yx = np.kron(y, x) xy = np.kron(x, y) expected = scipy.linalg.expm(-0.5j * angle_rads * (yx - xy)) assert np.allclose(actual, expected)
def test_givens_rotation_unitary(angle_rads): actual = cirq.unitary(cirq.givens(angle_rads)) c = np.cos(angle_rads) s = np.sin(angle_rads) # yapf: disable expected = np.array([[1, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], [0, 0, 0, 1]]) # yapf: enable assert np.allclose(actual, expected)
def test_givens_rotation_equivalent_circuit(): angle_rads = 3 * np.pi / 7 t = 2 * angle_rads / np.pi gate = cirq.givens(angle_rads) q0, q1 = cirq.LineQubit.range(2) equivalent_circuit = cirq.Circuit([ cirq.T(q0), cirq.T(q1)**-1, cirq.ISWAP(q0, q1)**t, cirq.T(q0)**-1, cirq.T(q1) ]) assert np.allclose(cirq.unitary(gate), cirq.unitary(equivalent_circuit))
def test_givens_rotation(): """Test if the sqrt_iswap synthesis for a givens rotation is correct""" thetas = np.linspace(0, 2 * np.pi, 100) qubits = [cirq.NamedQubit('a'), cirq.NamedQubit('b')] for theta in thetas: program = cirq.Circuit(cirq.givens(theta).on(qubits[0], qubits[1])) unitary = cirq.unitary(program) test_program = program.copy() cgoc.ConvertToSqrtIswapGates().optimize_circuit(test_program) test_unitary = cirq.unitary(test_program) np.testing.assert_allclose( 4, np.abs(np.trace(np.conjugate(np.transpose(test_unitary)) @ unitary)) )
def test_cirq_qsim_all_supported_gates(self): q0 = cirq.GridQubit(1, 1) q1 = cirq.GridQubit(1, 0) q2 = cirq.GridQubit(0, 1) q3 = cirq.GridQubit(0, 0) circuit = cirq.Circuit( cirq.Moment([ cirq.H(q0), cirq.H(q1), cirq.H(q2), cirq.H(q3), ]), cirq.Moment([ cirq.T(q0), cirq.T(q1), cirq.T(q2), cirq.T(q3), ]), cirq.Moment([ cirq.CZPowGate(exponent=0.7, global_shift=0.2)(q0, q1), cirq.CXPowGate(exponent=1.2, global_shift=0.4)(q2, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=0.3, global_shift=1.1)(q0), cirq.YPowGate(exponent=0.4, global_shift=1)(q1), cirq.ZPowGate(exponent=0.5, global_shift=0.9)(q2), cirq.HPowGate(exponent=0.6, global_shift=0.8)(q3), ]), cirq.Moment([ cirq.CX(q0, q2), cirq.CZ(q1, q3), ]), cirq.Moment([ cirq.X(q0), cirq.Y(q1), cirq.Z(q2), cirq.S(q3), ]), cirq.Moment([ cirq.XXPowGate(exponent=0.4, global_shift=0.7)(q0, q1), cirq.YYPowGate(exponent=0.8, global_shift=0.5)(q2, q3), ]), cirq.Moment([cirq.I(q0), cirq.I(q1), cirq.IdentityGate(2)(q2, q3)]), cirq.Moment([ cirq.rx(0.7)(q0), cirq.ry(0.2)(q1), cirq.rz(0.4)(q2), cirq.PhasedXPowGate(phase_exponent=0.8, exponent=0.6, global_shift=0.3)(q3), ]), cirq.Moment([ cirq.ZZPowGate(exponent=0.3, global_shift=1.3)(q0, q2), cirq.ISwapPowGate(exponent=0.6, global_shift=1.2)(q1, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=0.1, global_shift=0.9)(q0), cirq.YPowGate(exponent=0.2, global_shift=1)(q1), cirq.ZPowGate(exponent=0.3, global_shift=1.1)(q2), cirq.HPowGate(exponent=0.4, global_shift=1.2)(q3), ]), cirq.Moment([ cirq.SwapPowGate(exponent=0.2, global_shift=0.9)(q0, q1), cirq.PhasedISwapPowGate(phase_exponent=0.8, exponent=0.6)(q2, q3), ]), cirq.Moment([ cirq.PhasedXZGate(x_exponent=0.2, z_exponent=0.3, axis_phase_exponent=1.4)(q0), cirq.T(q1), cirq.H(q2), cirq.S(q3), ]), cirq.Moment([ cirq.SWAP(q0, q2), cirq.XX(q1, q3), ]), cirq.Moment([ cirq.rx(0.8)(q0), cirq.ry(0.9)(q1), cirq.rz(1.2)(q2), cirq.T(q3), ]), cirq.Moment([ cirq.YY(q0, q1), cirq.ISWAP(q2, q3), ]), cirq.Moment([ cirq.T(q0), cirq.Z(q1), cirq.Y(q2), cirq.X(q3), ]), cirq.Moment([ cirq.FSimGate(0.3, 1.7)(q0, q2), cirq.ZZ(q1, q3), ]), cirq.Moment([ cirq.ry(1.3)(q0), cirq.rz(0.4)(q1), cirq.rx(0.7)(q2), cirq.S(q3), ]), cirq.Moment([ cirq.MatrixGate( np.array([[0, -0.5 - 0.5j, -0.5 - 0.5j, 0], [0.5 - 0.5j, 0, 0, -0.5 + 0.5j], [0.5 - 0.5j, 0, 0, 0.5 - 0.5j], [0, -0.5 - 0.5j, 0.5 + 0.5j, 0]]))(q0, q1), cirq.MatrixGate( np.array([[0.5 - 0.5j, 0, 0, -0.5 + 0.5j], [0, 0.5 - 0.5j, -0.5 + 0.5j, 0], [0, -0.5 + 0.5j, -0.5 + 0.5j, 0], [0.5 - 0.5j, 0, 0, 0.5 - 0.5j]]))(q2, q3), ]), cirq.Moment([ cirq.MatrixGate(np.array([[1, 0], [0, 1j]]))(q0), cirq.MatrixGate(np.array([[0, -1j], [1j, 0]]))(q1), cirq.MatrixGate(np.array([[0, 1], [1, 0]]))(q2), cirq.MatrixGate(np.array([[1, 0], [0, -1]]))(q3), ]), cirq.Moment([ cirq.riswap(0.7)(q0, q1), cirq.givens(1.2)(q2, q3), ]), cirq.Moment([ cirq.H(q0), cirq.H(q1), cirq.H(q2), cirq.H(q3), ]), ) simulator = cirq.Simulator() cirq_result = simulator.simulate(circuit) qsim_simulator = qsimcirq.QSimSimulator() qsim_result = qsim_simulator.simulate(circuit) assert cirq.linalg.allclose_up_to_global_phase( qsim_result.state_vector(), cirq_result.state_vector())
def test_givens_rotation_has_consistent_protocols(angle_rads): cirq.testing.assert_implements_consistent_protocols( cirq.givens(angle_rads))
def test_givens_rotation_has_consistent_protocols(angle_rads): cirq.testing.assert_implements_consistent_protocols( cirq.givens(angle_rads), ignoring_global_phase=False)
assert all( isinstance(e.gate, cirq_google.SycamoreGate) for e in multi_qubit_ops) @pytest.mark.parametrize( 'gate', [ cirq.MatrixGate(cirq.unitary(cirq.CX), qid_shape=(2, 2)), cirq.ISWAP, cirq.SWAP, cirq.CNOT, cirq.CZ, cirq.PhasedISwapPowGate(exponent=1.0), cirq.PhasedISwapPowGate(exponent=1.0, phase_exponent=0.33), cirq.PhasedISwapPowGate(exponent=0.66, phase_exponent=0.25), *[cirq.givens(theta) for theta in np.linspace(0, 2 * np.pi, 30)], *[ cirq.ZZPowGate(exponent=2 * phi / np.pi) for phi in np.linspace(0, 2 * np.pi, 30) ], *[ cirq.CZPowGate(exponent=phi / np.pi) for phi in np.linspace(0, 2 * np.pi, 30) ], ], ) def test_convert_to_sycamore_equivalent_unitaries(gate): circuit = cirq.Circuit(gate.on(*cirq.LineQubit.range(2))) converted_circuit = cirq.optimize_for_target_gateset( circuit, gateset=cirq_google.SycamoreTargetGateset()) cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(
def _create_moment_operations( qubits: List[cirq.Qid], moment: Iterable[Tuple[int, int, float]]) -> List[cirq.Operation]: return [ cirq.givens(theta).on(qubits[a], qubits[b]) for a, b, theta in moment ]
def test_deprecated_givens_rotation(angle_rads): assert np.all( cirq.unitary(cirq.GivensRotation(angle_rads)) == cirq.unitary( cirq.givens(angle_rads)))
def test_compare_ryxxy_to_cirq_equivalent(rads): old_gate = openfermion.Ryxxy(rads=rads) new_gate = cirq.givens(angle_rads=rads) np.testing.assert_allclose(cirq.unitary(old_gate), cirq.unitary(new_gate))