Esempio n. 1
0
def test_xx_repr():
    assert repr(cirq.XXPowGate()) == 'cirq.XX'
    assert repr(cirq.XXPowGate(exponent=0.5)) == '(cirq.XX**0.5)'

    ms = cirq.XXPowGate(global_shift=-0.5)
    assert (repr(ms) == 'cirq.ms(np.pi/2)')
    assert (repr(ms**2) == 'cirq.ms(2.0*np.pi/2)')
    assert (repr(ms**-0.5) == 'cirq.ms(-0.5*np.pi/2)')
Esempio n. 2
0
def test_xx_str():
    assert str(cirq.XX) == 'XX'
    assert str(cirq.XX**0.5) == 'XX**0.5'
    assert str(cirq.XXPowGate(global_shift=0.1)) == 'XX'

    ms = cirq.XXPowGate(global_shift=-0.5)
    assert str(ms) == 'MS(π/2)'
    assert str(ms**2) == 'MS(2.0π/2)'
    assert str(ms**-1) == 'MS(-1.0π/2)'
Esempio n. 3
0
def test_xx_eq():
    eq = cirq.testing.EqualsTester()
    eq.add_equality_group(cirq.XX, cirq.XXPowGate(),
                          cirq.XXPowGate(exponent=1, global_shift=0),
                          cirq.XXPowGate(exponent=3, global_shift=0))
    eq.add_equality_group(cirq.XX**0.5, cirq.XX**2.5, cirq.XX**4.5)
    eq.add_equality_group(cirq.XX**0.25, cirq.XX**2.25, cirq.XX**-1.75)

    iXX = cirq.XXPowGate(global_shift=0.5)
    eq.add_equality_group(iXX**0.5, iXX**4.5)
    eq.add_equality_group(iXX**2.5, iXX**6.5)
Esempio n. 4
0
def test_xx_consistent():
    cirq.testing.assert_eigen_gate_has_consistent_apply_unitary(cirq.XXPowGate)

    cases = [
        cirq.XX, cirq.XX**0.1,
        cirq.XXPowGate(global_shift=0.1, exponent=0.2),
        cirq.XXPowGate(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)
Esempio n. 5
0
    def test_xx_qiskit(self):
        """Test the special XX (modified from cirq XXPowGate) 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.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])
        z_circuit = Circuit(circ)
        u1 = z_circuit.to_unitary()

        circ2 = cirq.XXPowGate(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))
Esempio n. 6
0
    def test_xx_pyquil(self):
        """Test the special XX (modified from cirq XXPowGate) for pyquil
        """

        #random.seed(RNDSEED)
        beta = random.uniform(-1, 1)  # rotation angles (in multiples of pi)
        circ1 = pyquil.quil.Program(H(0), H(1), CNOT(0,
                                                     1), RZ(beta * 2 * pi, 1),
                                    CNOT(0, 1), H(0), H(1))

        cirq_gate = cirq.XXPowGate(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))
Esempio n. 7
0
    def test_xx_cirq(self):
        """Test the special XX (modified from cirq XXPowGate) for cirq
        """

        #random.seed(RNDSEED)
        beta = random.uniform(-1,
                              1)  # we want to evolve under XX for time beta*pi
        cirq_gate = cirq.XXPowGate(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)
Esempio n. 8
0
 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)
Esempio n. 9
0
def create_mixer_ham(qubits_a, qubits_b, qubit_number, parameter_list):

    # Implements the exp(ZZ) operation on all entangled states
    for i in range(0, qubit_number):
        yield cirq.ZZPowGate(exponent= 2*parameter_list[0]/math.pi, global_shift= -0.5).on(qubits_a[i], qubits_b[i])

    # Implements the exp(XX) operation on all entangled states
    for i in range(0, qubit_number):
        yield cirq.XXPowGate(exponent= 2*parameter_list[1]/math.pi, global_shift= -0.5).on(qubits_a[i], qubits_b[i])
Esempio n. 10
0
def create_cost_ham(qubits_x, qubit_number, parameter_list):

    # We'll start by experimenting with a simple Ising model. First, we apply the transverse field

    for i in range(0, qubit_number):
        yield cirq.Rz(-2*transverse_field_strength*parameter_list[0]).on(qubits_x[i])

    # We can now apply neighbour interactions between qubits

    for i in range(0, qubit_number-1):
        yield cirq.XXPowGate(exponent= 2*parameter_list[1]/math.pi, global_shift=-0.5).on(qubits_x[i], qubits_x[i+1])
Esempio n. 11
0
def test_xx_repr():
    assert repr(cirq.XXPowGate()) == 'cirq.XX'
    assert repr(cirq.XXPowGate(exponent=0.5)) == '(cirq.XX**0.5)'

    cirq.testing.assert_equivalent_repr(cirq.XX)
    cirq.testing.assert_equivalent_repr(cirq.XX**0.1)
    cirq.testing.assert_equivalent_repr(cirq.XX**0.5)
    cirq.testing.assert_equivalent_repr(cirq.XX**2)
    cirq.testing.assert_equivalent_repr(
        cirq.XXPowGate(exponent=0.2, global_shift=0.3))

    ms = cirq.XXPowGate(global_shift=-0.5)
    assert (repr(ms) == 'cirq.MS(np.pi/2)')
    assert (repr(ms**2) == 'cirq.MS(2.0*np.pi/2)')
    assert (repr(ms**-0.5) == 'cirq.MS(-0.5*np.pi/2)')

    cirq.testing.assert_equivalent_repr(ms)
    cirq.testing.assert_equivalent_repr(ms**0.1)
    cirq.testing.assert_equivalent_repr(ms**0.5)
    cirq.testing.assert_equivalent_repr(ms**2)
Esempio n. 12
0
def test_xx_matrix():
    np.testing.assert_allclose(cirq.unitary(cirq.XX),
                               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.XX**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.XXPowGate(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)
Esempio n. 13
0
def test_xx_diagrams():
    a = cirq.NamedQubit('a')
    b = cirq.NamedQubit('b')
    circuit = cirq.Circuit(
        cirq.XX(a, b),
        cirq.XX(a, b)**3,
        cirq.XX(a, b)**0.5,
        cirq.XXPowGate(global_shift=-0.5).on(a, b),
    )
    cirq.testing.assert_has_diagram(
        circuit, """
a: ───XX───XX───XX───────MS(0.5π)───
      │    │    │        │
b: ───XX───XX───XX^0.5───MS(0.5π)───
""")
Esempio n. 14
0
def test_serialize_xx_pow_gate():
    q0, q1 = cirq.LineQubit.range(2)
    serializer = ionq.Serializer()
    for exponent in (0.5, 1.0, 1.5):
        circuit = cirq.Circuit(cirq.XXPowGate(exponent=exponent)(q0, q1))
        result = serializer.serialize(circuit)
        assert result == {
            'qubits':
            2,
            'circuit': [{
                'gate': 'xx',
                'targets': [0, 1],
                'rotation': exponent * np.pi
            }]
        }
def test_controlled_circuit_operation_is_consistent():
    op = cirq.CircuitOperation(
        cirq.FrozenCircuit(
            cirq.XXPowGate(exponent=0.25, global_shift=-0.5).on(*cirq.LineQubit.range(2))
        )
    )
    cb = cirq.NamedQubit('ctr')
    cop = cirq.ControlledOperation([cb], op)
    cirq.testing.assert_implements_consistent_protocols(cop, exponents=(-1, 1, 2))
    cirq.testing.assert_decompose_ends_at_default_gateset(cop)

    cop = cirq.ControlledOperation([cb], op, control_values=[0])
    cirq.testing.assert_implements_consistent_protocols(cop, exponents=(-1, 1, 2))
    cirq.testing.assert_decompose_ends_at_default_gateset(cop)

    cop = cirq.ControlledOperation([cb], op, control_values=[(0, 1)])
    cirq.testing.assert_implements_consistent_protocols(cop, exponents=(-1, 1, 2))
    cirq.testing.assert_decompose_ends_at_default_gateset(cop)
Esempio n. 16
0
def test_MS_arguments():
    eq_tester = cirq.testing.EqualsTester()
    eq_tester.add_equality_group(cirq.MS(np.pi / 2),
                                 cirq.XXPowGate(global_shift=-0.5))
Esempio n. 17
0
    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())
Esempio n. 18
0
    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())
Esempio n. 19
0
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
Esempio n. 20
0
    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])
Esempio n. 21
0
def test_ms_arguments():
    eq_tester = cirq.testing.EqualsTester()
    eq_tester.add_equality_group(cirq.ms(np.pi / 2), cirq.ion.ion_gates.MSGate(rads=np.pi / 2))
    eq_tester.add_equality_group(cirq.XXPowGate(global_shift=-0.5))
Esempio n. 22
0
def test_xx_repr():
    assert repr(cirq.XXPowGate()) == 'cirq.XX'
    assert repr(cirq.XXPowGate(exponent=0.5)) == '(cirq.XX**0.5)'
Esempio n. 23
0
def test_xx_str():
    assert str(cirq.XX) == 'XX'
    assert str(cirq.XX**0.5) == 'XX**0.5'
    assert str(cirq.XXPowGate(global_shift=0.1)) == 'XX'
Esempio n. 24
0
def test_xx_init():
    assert cirq.XXPowGate(exponent=1).exponent == 1
    v = cirq.XXPowGate(exponent=0.5)
    assert v.exponent == 0.5
Esempio n. 25
0
        (cirq.X, True),
        (cirq.X**0.5, True),
        (cirq.rx(np.pi), True),
        (cirq.rx(np.pi / 2), True),
        (cirq.Z, True),
        (cirq.H, True),
        (cirq.CNOT, True),
        (cirq.SWAP, True),
        (cirq.CCZ, True),
        (cirq.ControlledGate(cirq.ControlledGate(cirq.CCZ)), True),
        (GateUsingWorkspaceForApplyUnitary(), True),
        (GateAllocatingNewSpaceForResult(), True),
        (cirq.IdentityGate(qid_shape=(3, 4)), True),
        (
            cirq.ControlledGate(
                cirq.XXPowGate(exponent=0.25, global_shift=-0.5),
                num_controls=2,
                control_values=(1, (1, 0)),
            ),
            True,
        ),
        # Single qudit gate with dimension 4.
        (cirq.MatrixGate(np.kron(*(cirq.unitary(cirq.H), ) * 2),
                         qid_shape=(4, )), False),
        (cirq.MatrixGate(cirq.testing.random_unitary(
            4, random_state=1234)), False),
        (cirq.XX**sympy.Symbol("s"), True),
        (cirq.CZ**sympy.Symbol("s"), True),
    ],
)
def test_controlled_gate_is_consistent(gate: cirq.Gate,
Esempio n. 26
0
    cirq.TOFFOLI,
    'TwoQubitMatrixGate':
    cirq.TwoQubitMatrixGate(np.eye(4)),
    'UNCONSTRAINED_DEVICE':
    cirq.UNCONSTRAINED_DEVICE,
    '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