def test_yy_str(): assert str(cirq.YY) == 'YY' assert str(cirq.YY**0.5) == 'YY**0.5' assert str(cirq.YYPowGate(global_shift=0.1)) == 'YY' iYY = cirq.YYPowGate(global_shift=0.5) assert str(iYY) == 'YY'
def test_yy_repr(): assert repr(cirq.YYPowGate()) == 'cirq.YY' assert repr(cirq.YYPowGate(exponent=0.5)) == '(cirq.YY**0.5)' cirq.testing.assert_equivalent_repr(cirq.YY) cirq.testing.assert_equivalent_repr(cirq.YY**0.1) cirq.testing.assert_equivalent_repr(cirq.YY**0.5) cirq.testing.assert_equivalent_repr(cirq.YY**2) cirq.testing.assert_equivalent_repr( cirq.YYPowGate(exponent=0.2, global_shift=0.3))
def test_yy_eq(): eq = cirq.testing.EqualsTester() eq.add_equality_group(cirq.YY, cirq.YYPowGate(), cirq.YYPowGate(exponent=1, global_shift=0), cirq.YYPowGate(exponent=3, global_shift=0)) eq.add_equality_group(cirq.YY**0.5, cirq.YY**2.5, cirq.YY**4.5) eq.add_equality_group(cirq.YY**0.25, cirq.YY**2.25, cirq.YY**-1.75) iYY = cirq.YYPowGate(global_shift=0.5) eq.add_equality_group(iYY**0.5, iYY**4.5) eq.add_equality_group(iYY**2.5, iYY**6.5)
def test_yy_consistent(): cirq.testing.assert_eigen_gate_has_consistent_apply_unitary(cirq.YYPowGate) cases = [ cirq.YY, cirq.YY**0.1, cirq.YYPowGate(global_shift=0.1, exponent=0.2), cirq.YYPowGate(global_shift=-0.5, exponent=0.4) ] for case in cases: cirq.testing.assert_qasm_is_consistent_with_unitary(case) cirq.testing.assert_decompose_is_consistent_with_unitary(case) cirq.testing.assert_phase_by_is_consistent_with_unitary(case)
def _decompose_(self, qubits) -> 'cirq.OP_TREE': a, b = qubits xx = cirq.XXPowGate(exponent=self.theta / np.pi, global_shift=-0.5) yy = cirq.YYPowGate(exponent=self.theta / np.pi, global_shift=-0.5) yield xx(a, b) yield yy(a, b) yield cirq.CZ(a, b)**(-self.phi / np.pi)
def test_yy_qiskit(self): """Test the special YY (modified from cirq YYPowGate) for qiskit """ #random.seed(RNDSEED) beta = random.uniform(-1, 1) # rotation angles (in multiples of pi) qubits = qiskit.QuantumRegister(2) circ = qiskit.QuantumCircuit(qubits) circ.s(qubits[0]) circ.s(qubits[1]) circ.h(qubits[0]) circ.h(qubits[1]) circ.cx(qubits[0], qubits[1]) circ.rz(beta * 2 * pi, qubits[1]) circ.cx(qubits[0], qubits[1]) circ.h(qubits[0]) circ.h(qubits[1]) circ.rz(-pi / 2, qubits[0]) circ.rz(-pi / 2, qubits[1]) z_circuit = Circuit(circ) u1 = z_circuit.to_unitary() circ2 = cirq.YYPowGate(exponent=beta * 2) u2 = circ2._unitary_() u3 = [[cos(beta * pi), 0, 0, 1j * sin(beta * pi)], [0, cos(beta * pi), -1j * sin(beta * pi), 0], [0, -1j * sin(beta * pi), cos(beta * pi), 0], [1j * sin(beta * pi), 0, 0, cos(beta * pi)]] self.assertTrue(compare_unitary(u1, u2, tol=1e-10)) self.assertTrue(compare_unitary(u2, u3, tol=1e-10))
def test_yy_pyquil(self): """Test the special YY (modified from cirq YYPowGate) for pyquil """ #random.seed(RNDSEED) beta = random.uniform(-1, 1) # rotation angles (in multiples of pi) circ1 = pyquil.quil.Program(S(0), S(1),\ H(0),H(1),CNOT(0,1),RZ(beta*2*pi,1),CNOT(0,1),H(0),H(1),\ RZ(-pi/2,0), RZ(-pi/2,1)) cirq_gate = cirq.YYPowGate(exponent=beta * 2) cirq_circuit = cirq.Circuit() q = cirq.LineQubit(0) q2 = cirq.LineQubit(1) cirq_circuit.append(cirq_gate(q, q2)) z_circuit = Circuit(cirq_circuit) circ2 = z_circuit.to_pyquil() u1 = Circuit(circ1).to_unitary() u2 = Circuit(circ2).to_unitary() u3 = cirq_gate._unitary_() u4 = [[cos(beta * pi), 0, 0, 1j * sin(beta * pi)], [0, cos(beta * pi), -1j * sin(beta * pi), 0], [0, -1j * sin(beta * pi), cos(beta * pi), 0], [1j * sin(beta * pi), 0, 0, cos(beta * pi)]] self.assertTrue(compare_unitary(u1, u2, tol=1e-10)) self.assertTrue(compare_unitary(u2, u3, tol=1e-10)) self.assertTrue(compare_unitary(u3, u4, tol=1e-10))
def test_yy_cirq(self): """Test the special YY (modified from cirq YYPowGate) for cirq """ #random.seed(RNDSEED) beta = random.uniform(-1, 1) # we want to evolve under XX for time beta*pi cirq_gate = cirq.YYPowGate(exponent=beta * 2) cirq_circuit = cirq.Circuit() q = cirq.LineQubit(0) q2 = cirq.LineQubit(1) cirq_circuit.append(cirq_gate(q, q2)) circuit2 = Circuit(cirq_circuit) circuit3 = circuit2.to_cirq() U1 = Circuit(cirq2pyquil(cirq_circuit)).to_unitary() U2 = Circuit(cirq2pyquil(circuit3)).to_unitary() U3 = [[cos(beta * pi), 0, 0, 1j * sin(beta * pi)], [0, cos(beta * pi), -1j * sin(beta * pi), 0], [0, -1j * sin(beta * pi), cos(beta * pi), 0], [1j * sin(beta * pi), 0, 0, cos(beta * pi)]] if compare_unitary(U1, U2, tol=1e-10) == False: print(U1) print(U2) if compare_unitary(U2, U3, tol=1e-10) == False: print(U2) print(U3) self.assertTrue(compare_unitary(U1, U2, tol=1e-10), True) self.assertTrue(compare_unitary(U2, U3, tol=1e-10), True)
def test_yy_matrix(): np.testing.assert_allclose(cirq.unitary(cirq.YY), np.array([[0, 0, 0, -1], [0, 0, 1, 0], [0, 1, 0, 0], [-1, 0, 0, 0]]), atol=1e-8) np.testing.assert_allclose(cirq.unitary(cirq.YY**2), np.eye(4), atol=1e-8) c = np.cos(np.pi / 6) s = 1j * np.sin(np.pi / 6) np.testing.assert_allclose(cirq.unitary( cirq.YYPowGate(exponent=1 / 3, global_shift=-0.5)), np.array([[c, 0, 0, s], [0, c, -s, 0], [0, -s, c, 0], [s, 0, 0, c]]), atol=1e-8)
def test_cirq_qsim_global_shift(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.CXPowGate(exponent=1, global_shift=0.7)(q0, q1), cirq.CZPowGate(exponent=1, global_shift=0.9)(q2, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=1, global_shift=1.1)(q0), cirq.YPowGate(exponent=1, global_shift=1)(q1), cirq.ZPowGate(exponent=1, global_shift=0.9)(q2), cirq.HPowGate(exponent=1, global_shift=0.8)(q3), ]), cirq.Moment([ cirq.XXPowGate(exponent=1, global_shift=0.2)(q0, q1), cirq.YYPowGate(exponent=1, global_shift=0.3)(q2, q3), ]), cirq.Moment([ cirq.ZPowGate(exponent=0.25, global_shift=0.4)(q0), cirq.ZPowGate(exponent=0.5, global_shift=0.5)(q1), cirq.YPowGate(exponent=1, global_shift=0.2)(q2), cirq.ZPowGate(exponent=1, global_shift=0.3)(q3), ]), cirq.Moment([ cirq.ZZPowGate(exponent=1, global_shift=0.2)(q0, q1), cirq.SwapPowGate(exponent=1, global_shift=0.3)(q2, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=1, global_shift=0)(q0), cirq.YPowGate(exponent=1, global_shift=0)(q1), cirq.ZPowGate(exponent=1, global_shift=0)(q2), cirq.HPowGate(exponent=1, global_shift=0)(q3), ]), cirq.Moment([ cirq.ISwapPowGate(exponent=1, global_shift=0.3)(q0, q1), cirq.ZZPowGate(exponent=1, global_shift=0.5)(q2, q3), ]), cirq.Moment([ cirq.ZPowGate(exponent=0.5, global_shift=0)(q0), cirq.ZPowGate(exponent=0.25, global_shift=0)(q1), cirq.XPowGate(exponent=0.9, global_shift=0)(q2), cirq.YPowGate(exponent=0.8, global_shift=0)(q3), ]), cirq.Moment([ cirq.CZPowGate(exponent=0.3, global_shift=0)(q0, q1), cirq.CXPowGate(exponent=0.4, global_shift=0)(q2, q3), ]), cirq.Moment([ cirq.ZPowGate(exponent=1.3, global_shift=0)(q0), cirq.HPowGate(exponent=0.8, global_shift=0)(q1), cirq.XPowGate(exponent=0.9, global_shift=0)(q2), cirq.YPowGate(exponent=0.4, global_shift=0)(q3), ]), cirq.Moment([ cirq.XXPowGate(exponent=0.8, global_shift=0)(q0, q1), cirq.YYPowGate(exponent=0.6, global_shift=0)(q2, q3), ]), cirq.Moment([ cirq.HPowGate(exponent=0.7, global_shift=0)(q0), cirq.ZPowGate(exponent=0.2, global_shift=0)(q1), cirq.YPowGate(exponent=0.3, global_shift=0)(q2), cirq.XPowGate(exponent=0.7, global_shift=0)(q3), ]), cirq.Moment([ cirq.ZZPowGate(exponent=0.1, global_shift=0)(q0, q1), cirq.SwapPowGate(exponent=0.6, global_shift=0)(q2, q3), ]), cirq.Moment([ cirq.XPowGate(exponent=0.4, global_shift=0)(q0), cirq.YPowGate(exponent=0.3, global_shift=0)(q1), cirq.ZPowGate(exponent=0.2, global_shift=0)(q2), cirq.HPowGate(exponent=0.1, global_shift=0)(q3), ]), cirq.Moment([ cirq.ISwapPowGate(exponent=1.3, global_shift=0)(q0, q1), cirq.CXPowGate(exponent=0.5, global_shift=0)(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_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 _get_circuit_proto_pairs(): q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(0, 1) pairs = [ # HPOW and aliases. (cirq.Circuit(cirq.HPowGate(exponent=0.3)(q0)), _build_gate_proto("HP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.HPowGate(exponent=sympy.Symbol('alpha'))(q0)), _build_gate_proto("HP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.HPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0)), _build_gate_proto("HP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0'])), (cirq.Circuit(cirq.H(q0)), _build_gate_proto("HP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0'])), # XPOW and aliases. (cirq.Circuit(cirq.XPowGate(exponent=0.3)(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.XPowGate(exponent=sympy.Symbol('alpha'))(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.XPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0'])), (cirq.Circuit(cirq.X(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0'])), # YPOW and aliases (cirq.Circuit(cirq.YPowGate(exponent=0.3)(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.YPowGate(exponent=sympy.Symbol('alpha'))(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.YPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0'])), (cirq.Circuit(cirq.Y(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0'])), # ZPOW and aliases. (cirq.Circuit(cirq.ZPowGate(exponent=0.3)(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.ZPowGate(exponent=sympy.Symbol('alpha'))(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0'])), (cirq.Circuit(cirq.ZPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0'])), (cirq.Circuit(cirq.Z(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0'])), # XXPow and aliases (cirq.Circuit(cirq.XXPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("XXP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.XXPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("XXP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.XXPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("XXP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.XX(q0, q1)), _build_gate_proto("XXP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # YYPow and aliases (cirq.Circuit(cirq.YYPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("YYP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.YYPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("YYP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.YYPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("YYP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.YY(q0, q1)), _build_gate_proto("YYP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # ZZPow and aliases (cirq.Circuit(cirq.ZZPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("ZZP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.ZZPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("ZZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.ZZPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("ZZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.ZZ(q0, q1)), _build_gate_proto("ZZP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # CZPow and aliases (cirq.Circuit(cirq.CZPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("CZP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.CZPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("CZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.CZPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("CZP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.CZ(q0, q1)), _build_gate_proto("CZP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # CNOTPow and aliases (cirq.Circuit(cirq.CNotPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("CNP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.CNotPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("CNP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.CNotPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("CNP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.CNOT(q0, q1)), _build_gate_proto("CNP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # SWAPPow and aliases (cirq.Circuit(cirq.SwapPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("SP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.SwapPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("SP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.SwapPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("SP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.SWAP(q0, q1)), _build_gate_proto("SP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # ISWAPPow and aliases (cirq.Circuit(cirq.ISwapPowGate(exponent=0.3)(q0, q1)), _build_gate_proto("ISP", ['exponent', 'exponent_scalar', 'global_shift'], [0.3, 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.ISwapPowGate(exponent=sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("ISP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 1.0, 0.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.ISwapPowGate(exponent=3.1 * sympy.Symbol('alpha'))(q0, q1)), _build_gate_proto("ISP", ['exponent', 'exponent_scalar', 'global_shift'], ['alpha', 3.1, 0.0], ['0_0', '0_1'])), (cirq.Circuit(cirq.ISWAP(q0, q1)), _build_gate_proto("ISP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, 0.0], ['0_0', '0_1'])), # PhasedXPow and aliases (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=0.9, exponent=0.3, global_shift=0.2)(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], [0.9, 1.0, 0.3, 1.0, 0.2], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=sympy.Symbol('alpha'), exponent=0.3)(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], ['alpha', 1.0, 0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=3.1 * sympy.Symbol('alpha'), exponent=0.3)(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], ['alpha', 3.1, 0.3, 1.0, 0.0], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=0.9, exponent=sympy.Symbol('beta'))(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], [0.9, 1.0, 'beta', 1.0, 0.0], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=0.9, exponent=5.1 * sympy.Symbol('beta'))(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], [0.9, 1.0, 'beta', 5.1, 0.0], ['0_0'])), (cirq.Circuit( cirq.PhasedXPowGate(phase_exponent=3.1 * sympy.Symbol('alpha'), exponent=5.1 * sympy.Symbol('beta'))(q0)), _build_gate_proto("PXP", [ 'phase_exponent', 'phase_exponent_scalar', 'exponent', 'exponent_scalar', 'global_shift' ], ['alpha', 3.1, 'beta', 5.1, 0.0], ['0_0'])), # RX, RY, RZ with symbolization is tested in special cases as the # string comparison of the float converted sympy.pi does not happen # smoothly. See: test_serialize_deserialize_special_case_one_qubit (cirq.Circuit(cirq.rx(np.pi)(q0)), _build_gate_proto("XP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, -0.5], ['0_0'])), (cirq.Circuit(cirq.ry(np.pi)(q0)), _build_gate_proto("YP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, -0.5], ['0_0'])), (cirq.Circuit(cirq.rz(np.pi)(q0)), _build_gate_proto("ZP", ['exponent', 'exponent_scalar', 'global_shift'], [1.0, 1.0, -0.5], ['0_0'])), # Identity (cirq.Circuit(cirq.I(q0)), _build_gate_proto("I", ['unused'], [True], ['0_0'])), # FSimGate (cirq.Circuit(cirq.FSimGate(theta=0.1, phi=0.2)(q0, q1)), _build_gate_proto("FSIM", ['theta', 'theta_scalar', 'phi', 'phi_scalar'], [0.1, 1.0, 0.2, 1.0], ['0_0', '0_1'])), (cirq.Circuit( cirq.FSimGate(theta=2.1 * sympy.Symbol("alpha"), phi=1.3 * sympy.Symbol("beta"))(q0, q1)), _build_gate_proto("FSIM", ['theta', 'theta_scalar', 'phi', 'phi_scalar'], ['alpha', 2.1, 'beta', 1.3], ['0_0', '0_1'])), ] return pairs
def test_yy_repr(): assert repr(cirq.YYPowGate()) == 'cirq.YY' assert repr(cirq.YYPowGate(exponent=0.5)) == '(cirq.YY**0.5)'
def test_yy_init(): assert cirq.YYPowGate(exponent=1).exponent == 1 v = cirq.YYPowGate(exponent=0.5) assert v.exponent == 0.5
'WaitGate': cirq.WaitGate(cirq.Duration(nanos=10)), '_QubitAsQid': [ cirq.NamedQubit('a').with_dimension(5), cirq.GridQubit(1, 2).with_dimension(1) ], 'XPowGate': cirq.X**0.123, 'XX': cirq.XX, 'XXPowGate': [cirq.XXPowGate(), cirq.XX**0.123], 'YPowGate': cirq.Y**0.456, 'YY': cirq.YY, 'YYPowGate': [cirq.YYPowGate(), cirq.YY**0.456], 'ZPowGate': cirq.Z**0.789, 'ZZ': cirq.ZZ, 'ZZPowGate': [cirq.ZZPowGate(), cirq.ZZ**0.789], } SHOULDNT_BE_SERIALIZED = [ # Circuit optimizers are function-like. Only attributes # are ignore_failures, tolerance, and other feature flags 'ConvertToCzAndSingleGates', 'ConvertToIonGates', 'ConvertToNeutralAtomGates', 'ConvertToXmonGates',
def to_cirq(self, input_cirq_qubits=None): """Convert to a cirq gate. Args: input_cirq_qubits: list[cirq.LineQubit] (optional) a list of cirq Qubits that the gate can act on. If not provided the function will generate new cirq.LineQubit objects. Returns: A cirq Circuit object that corresponds to the specification of the quantum gate. In the special case the gate itself was natively generated from cirq, the function will faithfully reproduce the original GateOperation object, taking into account whether the gate acts on GridQubit objects or LineQubit objects. In the other cases the resulting cirq gate simply assumes that the qubits are LineQubit objects. """ if self.name not in ALL_GATES: sys.exit("Gate {} currently not supported.".format(self.name)) q_inds = [] q_inds.append(self.qubits[0].index) if len(self.qubits) >= 2: q_inds.append(self.qubits[1].index) if len(self.qubits) >= 3: q_inds.append(self.qubits[2].index) cirq_qubits = [] if input_cirq_qubits == None: for q in self.qubits: if q.info["label"] == "cirq": qkey = q.info["QubitKey"] if q.info["QubitType"] == "GridQubit": cirq_qubits.append(cirq.GridQubit(qkey[0], qkey[1])) if q.info["QubitType"] == "LineQubit": cirq_qubits.append(cirq.LineQubit(qkey)) else: cirq_qubits.append(cirq.LineQubit(q.index)) else: cirq_qubits = [ input_cirq_qubits[x] for x in [q.index for q in self.qubits] ] if len(self.params) > 0: params = self.params # single-qubit gates if self.name == "I": # Identity return cirq.I(cirq_qubits[0]) if self.name == "X": # Pauli X return cirq.X(cirq_qubits[0]) if self.name == "Y": # Pauli Y return cirq.Y(cirq_qubits[0]) if self.name == "Z": # Pauli Z return cirq.Z(cirq_qubits[0]) if self.name == "H": # Hadamard return cirq.H(cirq_qubits[0]) if self.name == "S": # S gate return cirq.S(cirq_qubits[0]) if self.name == "T": # T gate return cirq.T(cirq_qubits[0]) if self.name == "Rx": # Single-qubit X rotation return cirq.rx(params[0])(cirq_qubits[0]) if self.name == "Ry": # Single-qubit Y rotation return cirq.ry(params[0])(cirq_qubits[0]) if self.name == "Rz": # Single-qubit Z rotation return cirq.rz(params[0])(cirq_qubits[0]) if self.name == "PHASE": # Phase gate return cirq.Z(cirq_qubits[0])**(params[0] / pi) if self.name == "ZXZ": # PhasedXPowGate gate g = cirq.PhasedXPowGate(phase_exponent=params[0] / pi, exponent=params[1] / pi) return g(cirq_qubits[0]) if self.name == "RH": # HPowGate g = cirq.H**(params[0] / pi) return g(cirq_qubits[0]) if self.name == "Da": # Damping alpha gate g = DampingAlpha(params[0]) return g(cirq_qubits[0]) if self.name == "Db": # Damping beta gate g = DampingBeta(params[0]) return g(cirq_qubits[0]) # two-qubit gates if self.name == "CNOT": return cirq.CNOT(cirq_qubits[0], cirq_qubits[1]) if self.name == "CZ": return cirq.CZ(cirq_qubits[0], cirq_qubits[1]) if self.name == "CPHASE": return cirq.CZPowGate(exponent=params[0] / pi)(cirq_qubits[0], cirq_qubits[1]) if self.name == "SWAP": return cirq.SWAP(cirq_qubits[0], cirq_qubits[1]) if self.name == "ISWAP": return cirq.ISWAP(cirq_qubits[0], cirq_qubits[1]) if self.name == "XX": return cirq.XXPowGate(exponent=(params[0] * 2 / pi), global_shift=-1 / 2)(cirq_qubits[0], cirq_qubits[1]) if self.name == "YY": return cirq.YYPowGate(exponent=(params[0] * 2 / pi), global_shift=-1 / 2)(cirq_qubits[0], cirq_qubits[1]) if self.name == "ZZ": return cirq.ZZPowGate(exponent=(params[0] * 2 / pi), global_shift=-1 / 2)(cirq_qubits[0], cirq_qubits[1]) if self.name == "XY": return cirq.ISwapPowGate(exponent=(-params[0] * 4 / pi))( cirq_qubits[0], cirq_qubits[1])