def test_rh_pyquil(self): """Test the special RH (from cirq HPowGate) for pyquil """ #random.seed(RNDSEED) beta = random.uniform(-1, 1) # rotation angles (in multiples of pi) circ1 = pyquil.quil.Program(RY(-pi / 4, 0), RZ(beta * pi, 0), RY(pi / 4, 0)) cirq_gate = cirq.HPowGate(exponent=beta) cirq_circuit = cirq.Circuit() q = cirq.LineQubit(0) cirq_circuit.append([cirq_gate(q)], strategy=cirq.circuits.InsertStrategy.EARLIEST) z_circuit = Circuit(cirq_circuit) circ2 = z_circuit.to_pyquil() u1 = Circuit(circ1).to_unitary() u2 = Circuit(circ2).to_unitary() u3 = cirq_gate._unitary_() # desired operator elem00 = cos(beta * pi / 2) - 1j * 1 / np.sqrt(2) * sin(beta * pi / 2) elem01 = -1j * 1 / np.sqrt(2) * sin(beta * pi / 2) elem10 = -1j * 1 / np.sqrt(2) * sin(beta * pi / 2) elem11 = cos(beta * pi / 2) + 1j * 1 / np.sqrt(2) * sin(beta * pi / 2) u = np.array([[elem00, elem01], [elem10, elem11]]) self.assertTrue(compare_unitary(u1, u2, tol=1e-10)) self.assertTrue(compare_unitary(u2, u3, tol=1e-10)) self.assertTrue(compare_unitary(u3, u, tol=1e-10))
def test_rh_cirq(self): """Test the special RH (from cirq HPowGate) for cirq """ #random.seed(RNDSEED) beta = random.uniform(-1, 1) cirq_gate = cirq.HPowGate(exponent=beta) cirq_circuit = cirq.Circuit() q = cirq.LineQubit(0) cirq_circuit.append([cirq_gate(q)], strategy=cirq.circuits.InsertStrategy.EARLIEST) circuit2 = Circuit(cirq_circuit) circuit3 = circuit2.to_cirq() # desired operator elem00 = cos(beta * pi / 2) - 1j * 1 / np.sqrt(2) * sin(beta * pi / 2) elem01 = -1j * 1 / np.sqrt(2) * sin(beta * pi / 2) elem10 = -1j * 1 / np.sqrt(2) * sin(beta * pi / 2) elem11 = cos(beta * pi / 2) + 1j * 1 / np.sqrt(2) * sin(beta * pi / 2) u = np.array([[elem00, elem01], [elem10, elem11]]) U1 = Circuit(cirq2pyquil(cirq_circuit)).to_unitary() U2 = Circuit(cirq2pyquil(circuit3)).to_unitary() if compare_unitary(U1, U2, tol=1e-10) == False: print(cirq2pyquil(cirq_circuit)) print(cirq2pyquil(circuit3)) self.assertTrue(compare_unitary(U1, U2, tol=1e-10), True) self.assertTrue(compare_unitary(u, U1, tol=1e-10), True)
def test_serialize_controlled_circuit_unsupported_value(self): """Ensure serializing invalid controlled gates fails gracefully.""" qubits = cirq.GridQubit.rect(1, 2) bad_qubit = cirq.LineQubit(5) invalid_control = cirq.Circuit( cirq.H(qubits[0]).controlled_by(qubits[1], bad_qubit)) invalid_symbol = cirq.Circuit((cirq.HPowGate()( qubits[0])**(sympy.Symbol('alpha') + 1)).controlled_by(qubits[1])) with self.assertRaises(ValueError): serializer.serialize_circuit(invalid_control) with self.assertRaises(ValueError): serializer.serialize_circuit(invalid_symbol)
def test_serialize_circuit_unsupported_value(self): """Ensure we error on unsupported arithmetic expressions and qubits.""" q0 = cirq.GridQubit(0, 0) unsupported_circuit = cirq.Circuit( cirq.HPowGate()(q0)**(sympy.Symbol('alpha') + 1)) q1 = cirq.NamedQubit('wont work') unsupported_circuit2 = cirq.Circuit(cirq.H(q1)) with self.assertRaises(ValueError): serializer.serialize_circuit(unsupported_circuit) with self.assertRaises(ValueError): serializer.serialize_circuit(unsupported_circuit2)
def get_default_noise_dict(self) -> Dict[str, Any]: """Returns the current noise parameters""" default_noise_dict = { str(cirq.YPowGate()): cirq.depolarize(1e-2), str(cirq.ZPowGate()): cirq.depolarize(1e-2), str(cirq.XPowGate()): cirq.depolarize(1e-2), str(cirq.PhasedXPowGate(phase_exponent=0)): cirq.depolarize(1e-2), str(cirq.HPowGate(exponent=1)): cirq.depolarize(1e-2), str(cirq.CNotPowGate(exponent=1)): cirq.depolarize(3e-2), str(cirq.CZPowGate(exponent=1)): cirq.depolarize(3e-2), str(cirq.CCXPowGate(exponent=1)): cirq.depolarize(8e-2), str(cirq.CCZPowGate(exponent=1)): cirq.depolarize(8e-2), } return default_noise_dict
def test_rh_qiskit(self): """Test the special RH (from cirq HPowGate) for qiskit """ #random.seed(RNDSEED) beta = random.uniform(-1, 1) # rotation angles (in multiples of pi) qubits = qiskit.QuantumRegister(1) circ = qiskit.QuantumCircuit(qubits) circ.ry(-pi / 4, qubits[0]) circ.rz(beta * pi, qubits[0]) circ.ry(pi / 4, qubits[0]) z_circuit = Circuit(circ) u1 = z_circuit.to_unitary() circ2 = cirq.HPowGate(exponent=beta) u2 = circ2._unitary_() self.assertTrue(compare_unitary(u1, u2, tol=1e-10))
def test_h_init(): h = cirq.HPowGate(exponent=0.5) assert h.exponent == 0.5
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())
class SerializerTest(tf.test.TestCase, parameterized.TestCase): """Tests basic serializer functionality""" @parameterized.parameters( [{ 'circ_proto_pair': v } for v in _get_controlled_circuit_proto_pairs() + _get_circuit_proto_pairs() + _get_noise_proto_pairs()]) def test_serialize_circuit_valid(self, circ_proto_pair): """Test conversion of cirq Circuits to tfq_gate_set proto.""" self.assertProtoEquals( serializer.serialize_circuit(circ_proto_pair[0]), circ_proto_pair[1]) @parameterized.parameters( [{ 'circ_proto_pair': v } for v in _get_controlled_circuit_proto_pairs() + _get_circuit_proto_pairs() + _get_noise_proto_pairs()]) def test_deserialize_circuit_valid(self, circ_proto_pair): """Test deserialization of protos in tfq_gate_set.""" # String casting is done here to round floating point values. # cirq.testing.assert_same_circuits will call break and think # cirq.Z^0.30000001 is different from cirq.Z^0.3 self.assertEqual(circ_proto_pair[0], serializer.deserialize_circuit(circ_proto_pair[1])) @parameterized.parameters( [{ 'circ_proto_pair': v } for v in _get_controlled_circuit_proto_pairs() + _get_circuit_proto_pairs() + _get_noise_proto_pairs()]) def test_serialize_deserialize_circuit_consistency(self, circ_proto_pair): """Ensure that serializing followed by deserializing works.""" # String casting is done here to round floating point values. # cirq.testing.assert_same_circuits will call break and think # cirq.Z^0.30000001 is different from cirq.Z^0.3 self.assertProtoEquals( serializer.serialize_circuit( serializer.deserialize_circuit(circ_proto_pair[1])), circ_proto_pair[1]) self.assertEqual( serializer.deserialize_circuit( serializer.serialize_circuit(circ_proto_pair[0])), circ_proto_pair[0]) def test_serialize_circuit_unsupported_gate(self): """Ensure we error on unsupported gates.""" q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(0, 1) unsupported_circuit = cirq.Circuit(cirq.QFT(q0, q1)) with self.assertRaises(ValueError): serializer.serialize_circuit(unsupported_circuit) def test_serialize_circuit_with_large_identity(self): """Ensure that multi qubit identity errors correctly.""" q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(0, 1) unsupported_circuit = cirq.Circuit( cirq.IdentityGate(num_qubits=2)(q0, q1)) with self.assertRaisesRegex(ValueError, expected_regex="cirq.I"): serializer.serialize_circuit(unsupported_circuit) @parameterized.parameters([ { "gate_with_param": g(p) } # Use a gate from each category of serializer for g in [ # eigen lambda p: cirq.Circuit( cirq.HPowGate(exponent=p, global_shift=p) (cirq.GridQubit(0, 0))), # phased eigen lambda p: cirq.Circuit( cirq.PhasedXPowGate( phase_exponent=p, exponent=p, global_shift=p) (cirq.GridQubit(0, 0))), # fsim lambda p: cirq.Circuit( cirq.FSimGate(theta=p, phi=p) (cirq.GridQubit(0, 0), cirq.GridQubit(0, 1))), ] # Attempt parameterization with a variety of numeric types for p in [0.35, float(0.35), 35e-2, np.float32(0.35), np.float64(0.35), 7] ]) def test_serialize_circuit_valid_number_types(self, gate_with_param): """Tests number datatype support by our serializer.""" self.assertAllClose( gate_with_param.unitary(), serializer.deserialize_circuit( serializer.serialize_circuit(gate_with_param)).unitary()) def test_serialize_circuit_unsupported_value(self): """Ensure we error on unsupported arithmetic expressions and qubits.""" q0 = cirq.GridQubit(0, 0) unsupported_circuit = cirq.Circuit( cirq.HPowGate()(q0)**(sympy.Symbol('alpha') + 1)) q1 = cirq.NamedQubit('wont work') unsupported_circuit2 = cirq.Circuit(cirq.H(q1)) with self.assertRaises(ValueError): serializer.serialize_circuit(unsupported_circuit) with self.assertRaises(ValueError): serializer.serialize_circuit(unsupported_circuit2) def test_serialize_controlled_circuit_unsupported_value(self): """Ensure serializing invalid controlled gates fails gracefully.""" qubits = cirq.GridQubit.rect(1, 2) bad_qubit = cirq.LineQubit(5) invalid_control = cirq.Circuit( cirq.H(qubits[0]).controlled_by(qubits[1], bad_qubit)) invalid_symbol = cirq.Circuit((cirq.HPowGate()( qubits[0])**(sympy.Symbol('alpha') + 1)).controlled_by(qubits[1])) with self.assertRaises(ValueError): serializer.serialize_circuit(invalid_control) with self.assertRaises(ValueError): serializer.serialize_circuit(invalid_symbol) def test_serialize_noise_channel_unsupported_value(self): """Ensure serializing invalid channels fails gracefully.""" qubit = cirq.LineQubit(5) simple_circuit = cirq.Circuit(cirq.depolarize(0.3)(qubit)) with self.assertRaises(ValueError): serializer.serialize_circuit(simple_circuit) @parameterized.parameters([{'inp': v} for v in ['wrong', 1.0, None, []]]) def test_serialize_circuit_wrong_type(self, inp): """Attempt to serialize invalid objects types.""" with self.assertRaises(TypeError): serializer.serialize_circuit(input) @parameterized.parameters([{'inp': v} for v in ['wrong', 1.0, None, []]]) def test_deserialize_circuit_wrong_type(self, inp): """Attempt to deserialize invalid objects types.""" with self.assertRaises(TypeError): serializer.deserialize_circuit(input) @parameterized.parameters([{'inp': v} for v in ['wrong', 1.0, None, []]]) def test_serialize_paulisum_wrong_type(self, inp): """Attempt to serialize invalid object types.""" with self.assertRaises(TypeError): serializer.serialize_paulisum(inp) @parameterized.parameters([{'inp': v} for v in ['wrong', 1.0, None, []]]) def test_deserialize_paulisum_wrong_type(self, inp): """Attempt to deserialize invalid object types.""" with self.assertRaises(TypeError): serializer.deserialize_paulisum(inp) def test_serialize_paulisum_invalid(self): """Ensure we don't support anything but GridQubits.""" q0 = cirq.NamedQubit('wont work') a = 3.0 * cirq.Z(q0) - 2.0 * cirq.X(q0) with self.assertRaises(ValueError): serializer.serialize_paulisum(a) @parameterized.parameters([{ 'sum_proto_pair': v } for v in _get_valid_pauli_proto_pairs()]) def test_serialize_paulisum_simple(self, sum_proto_pair): """Ensure serialization is correct.""" self.assertProtoEquals( sum_proto_pair[1], serializer.serialize_paulisum(sum_proto_pair[0])) @parameterized.parameters([{ 'sum_proto_pair': v } for v in _get_valid_pauli_proto_pairs()]) def test_deserialize_paulisum_simple(self, sum_proto_pair): """Ensure deserialization is correct.""" self.assertEqual(serializer.deserialize_paulisum(sum_proto_pair[1]), sum_proto_pair[0]) @parameterized.parameters([{ 'sum_proto_pair': v } for v in _get_valid_pauli_proto_pairs()]) def test_serialize_deserialize_paulisum_consistency(self, sum_proto_pair): """Serialize and deserialize and ensure nothing changed.""" self.assertEqual( serializer.serialize_paulisum( serializer.deserialize_paulisum(sum_proto_pair[1])), sum_proto_pair[1]) self.assertEqual( serializer.deserialize_paulisum( serializer.serialize_paulisum(sum_proto_pair[0])), sum_proto_pair[0]) @parameterized.parameters([ { 'gate': cirq.rx(3.0 * sympy.Symbol('alpha')) }, { 'gate': cirq.ry(-1.0 * sympy.Symbol('alpha')) }, { 'gate': cirq.rz(sympy.Symbol('alpha')) }, ]) def test_serialize_deserialize_special_case_one_qubit(self, gate): """Check output state equality.""" q0 = cirq.GridQubit(0, 0) c = cirq.Circuit(gate(q0)) c = c._resolve_parameters_(cirq.ParamResolver({"alpha": 0.1234567})) before = c.unitary() c2 = serializer.deserialize_circuit(serializer.serialize_circuit(c)) after = c2.unitary() self.assertAllClose(before, after) def test_terminal_measurement_support(self): """Test that non-terminal measurements error during serialization.""" q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(0, 1) simple_circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0), cirq.H(q1), cirq.Z(q1), cirq.measure(q1)) simple_circuit_before_call = copy.deepcopy(simple_circuit) expected_circuit = cirq.Circuit(cirq.Moment([cirq.H(q0), cirq.H(q1)]), cirq.Moment([cirq.Z(q1)]), cirq.Moment([])) self.assertEqual(serializer.serialize_circuit(simple_circuit), serializer.serialize_circuit(expected_circuit)) # Check that serialization didn't modify existing circuit. self.assertEqual(simple_circuit, simple_circuit_before_call) invalid_circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0), cirq.measure(q0)) with self.assertRaisesRegex(ValueError, expected_regex="non-terminal"): serializer.serialize_circuit(invalid_circuit) def test_serialize_deserialize_identity(self): """Confirm that identity gates can be serialized and deserialized.""" q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(0, 1) paulisum_with_identity = cirq.PauliSum.from_pauli_strings([ cirq.PauliString(cirq.I(q0)), cirq.PauliString(cirq.Z(q0), cirq.Z(q1)), ]) self.assertEqual( paulisum_with_identity, serializer.deserialize_paulisum( serializer.serialize_paulisum(paulisum_with_identity)))
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
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import pytest import cirq import cirq_pasqal Q, Q2, Q3 = cirq.LineQubit.range(3) @pytest.mark.parametrize( "op,expected", [ (cirq.H(Q), True), (cirq.HPowGate(exponent=0.5)(Q), False), (cirq.PhasedXPowGate(exponent=0.25, phase_exponent=0.125)(Q), True), (cirq.XPowGate(exponent=0.5)(Q), True), (cirq.YPowGate(exponent=0.25)(Q), True), (cirq.ZPowGate(exponent=0.125)(Q), True), (cirq.CZPowGate(exponent=0.5)(Q, Q2), False), (cirq.CZ(Q, Q2), True), (cirq.CNOT(Q, Q2), True), (cirq.SWAP(Q, Q2), False), (cirq.ISWAP(Q, Q2), False), (cirq.CCNOT(Q, Q2, Q3), True), (cirq.CCZ(Q, Q2, Q3), True), (cirq.ParallelGate(cirq.X, num_copies=3)(Q, Q2, Q3), True), (cirq.ParallelGate(cirq.Y, num_copies=3)(Q, Q2, Q3), True), (cirq.ParallelGate(cirq.Z, num_copies=3)(Q, Q2, Q3), True), (cirq.X(Q).controlled_by(Q2, Q3), True),
import cirq import cirq_ionq as ionq VALID_GATES = ( cirq.X, cirq.Y, cirq.Z, cirq.X**0.5, cirq.Y**0.5, cirq.Z**0.5, cirq.rx(0.1), cirq.ry(0.1), cirq.rz(0.1), cirq.H, cirq.HPowGate(exponent=1, global_shift=-0.5), cirq.T, cirq.S, cirq.CNOT, cirq.CXPowGate(exponent=1, global_shift=-0.5), cirq.XX, cirq.YY, cirq.ZZ, cirq.XX**0.5, cirq.YY**0.5, cirq.ZZ**0.5, cirq.SWAP, cirq.SwapPowGate(exponent=1, global_shift=-0.5), cirq.MeasurementGate(num_qubits=1, key='a'), cirq.MeasurementGate(num_qubits=2, key='b'), cirq.MeasurementGate(num_qubits=10, key='c'),
cirq.YPowGate(exponent=0.71), cirq.PhasedXPowGate(phase_exponent=1.7, exponent=-0.58), cirq.Z, cirq.ZPowGate(exponent=-0.23), ] finally_decomposed_1q_gates = [] native_2q_gates = [ ig.IsingGate(exponent=0.45), ig.XYGate(exponent=0.38), ] non_native_1q_gates = [ cirq.H, cirq.HPowGate(exponent=-0.55), cirq.PhasedXZGate(x_exponent=0.2, z_exponent=-0.5, axis_phase_exponent=0.75), ] non_native_2q_gates = [ cirq.ISwapPowGate(exponent=0.27), cirq.ISWAP, cirq.SWAP, cirq.CNOT, cirq.CXPowGate(exponent=-2.2), cirq.CZ, cirq.CZPowGate(exponent=1.6), ]
'GateOperation': [ cirq.CCNOT(*cirq.LineQubit.range(3)), cirq.CCZ(*cirq.LineQubit.range(3)), cirq.CNOT(*cirq.LineQubit.range(2)), cirq.CSWAP(*cirq.LineQubit.range(3)), cirq.CZ(*cirq.LineQubit.range(2)) ], 'GeneralizedAmplitudeDampingChannel': cirq.GeneralizedAmplitudeDampingChannel(0.1, 0.2), 'GlobalPhaseOperation': cirq.GlobalPhaseOperation(-1j), 'GridQubit': cirq.GridQubit(10, 11), 'H': cirq.H, 'HPowGate': [cirq.HPowGate(exponent=-8), cirq.H**0.123], 'I': cirq.I, 'ISWAP': cirq.ISWAP, 'ISwapPowGate': [cirq.ISwapPowGate(exponent=-8), cirq.ISWAP**0.123], 'IdentityGate': [ cirq.I, cirq.IdentityGate(num_qubits=5), cirq.IdentityGate(num_qubits=5, qid_shape=(3, ) * 5) ], 'IdentityOperation': [ cirq.IdentityOperation(cirq.LineQubit.range(2)), cirq.IdentityOperation(cirq.LineQubit.range(5)) ], 'LineQubit': [cirq.LineQubit(0), cirq.LineQubit(123)],