def test_riswap_hamiltonian(angle_rads): actual = cirq.unitary(cirq.riswap(angle_rads)) x = np.array([[0, 1], [1, 0]]) y = np.array([[0, -1j], [1j, 0]]) xx = np.kron(x, x) yy = np.kron(y, y) expected = scipy.linalg.expm(+0.5j * angle_rads * (xx + yy)) assert np.allclose(actual, expected)
def test_riswap_unitary(angle_rads): actual = cirq.unitary(cirq.riswap(angle_rads)) c = np.cos(angle_rads) s = 1j * 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_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_deprecated_riswap(angle_rads): assert np.all( cirq.unitary(cirq.ISwapRotation(angle_rads)) == cirq.unitary( cirq.riswap(angle_rads)))
def test_riswap_has_consistent_protocols(angle_rads): cirq.testing.assert_implements_consistent_protocols( cirq.riswap(angle_rads), ignoring_global_phase=False)
import cirq from txtutils import ndtotext_print import numpy as np import sympy from tfq_diamond import U π = np.pi U_pi = U(π) iswap_1 = cirq.riswap(3 * π / 2) # Apposite sign to sympy!!! iswap_2 = cirq.riswap(π / 2) print('iSWAP(π/2) [NOT THE ONE WE USE, BUT MIGHT WORK ANYWAY!]') ndtotext_print(iswap_1._unitary_()) print('iSWAP(3π/2)') ndtotext_print(iswap_2._unitary_()) def Rn(n): return cirq.ZPowGate()**(2**(1 - n)) qc_test = cirq.Circuit() q = cirq.GridQubit(1, 1) qc_test.append(Rn(5).on(q)) print(qc_test) print(qc_test.unitary()) print('Should be:', np.exp(2 * π * 1j / 2**5)) assert np.isclose(qc_test.unitary()[1, 1], np.exp(2 * π * 1j / 2**5)) n_qft = 2
def test_riswap_has_consistent_protocols(angle_rads): cirq.testing.assert_implements_consistent_protocols( cirq.riswap(angle_rads))
import cirq from txtutils import ndtotext_print import numpy as np import sympy π = np.pi print('iSWAP(π/2) [NOT THE ONE WE USE, BUT MIGHT WORK ANYWAY!]') ndtotext_print(cirq.riswap(π / 2)._unitary_()) print('iSWAP(π/2) @ iSWAP(3π/2)') ndtotext_print( cirq.riswap(π / 2)._unitary_() @ cirq.riswap(3 * π / 2)._unitary_()) n_qft = 5 A = [cirq.GridQubit(0, i) for i in range(n_qft)] B = [cirq.GridQubit(1, i) for i in range(n_qft)] ''' A1 - B1 | | A2 - B2 | | A3 - B3 . . . ''' qc = cirq.Circuit() # Input data input_values = [1, 1, 1] for i, value in enumerate(input_values):
def test_deprecated_riswap(angle_rads): with capture_logging(): assert np.all( cirq.unitary(cirq.ISwapRotation(angle_rads)) == cirq.unitary( cirq.riswap(angle_rads)))