示例#1
0
def demo_adonis(do_measure=False, use_qsim=False):
    """Run the demo using the Adonis architecture."""

    print('\nAdonis demo\n===========\n')
    device = Adonis()
    qasm_program = """
        OPENQASM 2.0;
        include "qelib1.inc";
        qreg q[3];
        creg meas[3];
        U(0.2, 0.5, 1.7) q[1];
        h q[0];
        cx q[2], q[1];
        ising(-0.6) q[0], q[2];  // QASM extension
    """
    if do_measure:
        qasm_program += '\nmeasure q -> meas;'

    circuit = circuit_from_qasm(qasm_program)

    # add some more gates
    q0 = cirq.NamedQubit('q_0')
    q2 = cirq.NamedQubit('q_2')
    circuit.insert(len(circuit) - 1, cirq.CXPowGate(exponent=0.723)(q2, q0))

    demo(device, circuit, do_measure, use_qsim=use_qsim)
示例#2
0
def test_rot_gates_eq():
    eq = cirq.testing.EqualsTester()
    gates = [
        lambda p: cirq.CZ**p,
        lambda p: cirq.X**p,
        lambda p: cirq.Y**p,
        lambda p: cirq.Z**p,
        lambda p: cirq.CNOT**p,
    ]
    for gate in gates:
        eq.add_equality_group(gate(3.5), gate(-0.5))
        eq.make_equality_group(lambda: gate(0))
        eq.make_equality_group(lambda: gate(0.5))

    eq.add_equality_group(cirq.XPowGate(), cirq.XPowGate(exponent=1), cirq.X)
    eq.add_equality_group(cirq.YPowGate(), cirq.YPowGate(exponent=1), cirq.Y)
    eq.add_equality_group(cirq.ZPowGate(), cirq.ZPowGate(exponent=1), cirq.Z)
    eq.add_equality_group(cirq.ZPowGate(exponent=1, global_shift=-0.5),
                          cirq.ZPowGate(exponent=5, global_shift=-0.5))
    eq.add_equality_group(cirq.ZPowGate(exponent=3, global_shift=-0.5))
    eq.add_equality_group(cirq.ZPowGate(exponent=1, global_shift=-0.1))
    eq.add_equality_group(cirq.ZPowGate(exponent=5, global_shift=-0.1))
    eq.add_equality_group(cirq.CNotPowGate(), cirq.CXPowGate(),
                          cirq.CNotPowGate(exponent=1), cirq.CNOT)
    eq.add_equality_group(cirq.CZPowGate(), cirq.CZPowGate(exponent=1),
                          cirq.CZ)
示例#3
0
    def controlled(self,
                   num_controls: int = None,
                   control_values: Optional[Sequence[
                       Union[int, Collection[int]]]] = None,
                   control_qid_shape: Optional[Tuple[int, ...]] = None
                  ) -> raw_types.Gate:
        """
        Constructs CXPowGate from controlled XPowGate when applicable.

        This method is a specialized controlled method for XPowGate. It
        overrides the default behavior of returning a ControlledGate by
        transforming the underlying controlled gate to a CXPowGate and
        removing the last specified control qubit (which acts first
        semantically).  If this is a gate with multiple control qubits, it will
        now be a ControlledGate with one less control.

        This behavior only occurs when the last control qubit is a default-type
        control qubit. A default-type control qubit is one with shape of 2 (not
        a generic qudit) and where the control is satisfied by the qubit being
        ON, as opposed to OFF.

        (Note that a CXPowGate is, by definition, a controlled-XPowGate.)
        """
        result = super().controlled(num_controls, control_values,
                                    control_qid_shape)
        if (isinstance(result, controlled_gate.ControlledGate) and
                result.control_values[-1] == (1,) and
                result.control_qid_shape[-1] == 2):
            return cirq.CXPowGate(exponent=self._exponent,
                                  global_shift=self._global_shift).controlled(
                                      result.num_controls() - 1,
                                      result.control_values[:-1],
                                      result.control_qid_shape[:-1])
        return result
 def __build_mixing_layer(self, qubits, skip_factor):
     curent_skip = skip_factor
     current_qubit_index = 0
     circuit = cirq.Circuit()
     while current_qubit_index + skip_factor < len(qubits):
         while current_qubit_index + skip_factor < len(qubits) and curent_skip > 0:
             self.thetas += (sympy.symbols(f"{self.symbol_name_prefix}{len(self.thetas)}"),)
             circuit.append([cirq.CXPowGate(exponent=self.thetas[-1]).on(qubits[current_qubit_index],
                                                                         qubits[current_qubit_index + skip_factor])])
             curent_skip -= 1
             current_qubit_index += 1
         current_qubit_index += skip_factor
         curent_skip = skip_factor
     return circuit
示例#5
0
    def controlled(
            self,
            num_controls: int = None,
            control_values: Optional[Sequence[Union[int,
                                                    Collection[int]]]] = None,
            control_qid_shape: Optional[Tuple[int,
                                              ...]] = None) -> raw_types.Gate:
        """
        Returns a controlled `XPowGate`, using a `CXPowGate` where possible.

        The `controlled` method of the `Gate` class, of which this class is a
        child, returns a `ControlledGate`. This method overrides this behavior
        to return a `CXPowGate` or a `ControlledGate` of a `CXPowGate`, when
        this is possible.

        The conditions for the override to occur are:
            * The `global_shift` of the `XPowGate` is 0.
            * The `control_values` and `control_qid_shape` are compatible with
                the `CXPowGate`:
                * The last value of `control_qid_shape` is a qubit.
                * The last value of `control_values` corresponds to the
                    control being satisfied if that last qubit is 1 and
                    not satisfied if the last qubit is 0.

        If these conditions are met, then the returned object is a `CXPowGate`
        or, in the case that there is more than one controlled qudit, a
        `ControlledGate` with the `Gate` being a `CXPowGate`. In the
        latter case the `ControlledGate` is controlled by one less qudit
        than specified in `control_values` and `control_qid_shape` (since
        one of these, the last qubit, is used as the control for the
        `CXPowGate`).

        If the above conditions are not met, a `ControlledGate` of this
        gate will be returned.
        """
        result = super().controlled(num_controls, control_values,
                                    control_qid_shape)
        if (self._global_shift == 0
                and isinstance(result, controlled_gate.ControlledGate)
                and result.control_values[-1] == (1, )
                and result.control_qid_shape[-1] == 2):
            return cirq.CXPowGate(exponent=self._exponent,
                                  global_shift=self._global_shift).controlled(
                                      result.num_controls() - 1,
                                      result.control_values[:-1],
                                      result.control_qid_shape[:-1])
        return result
示例#6
0
def demo_apollo(do_measure=False, use_qsim=False):
    """Run the demo using the Apollo architecture."""

    print('\nApollo demo\n===========\n')
    device = Apollo()
    qasm_program = """
        OPENQASM 2.0;
        include "qelib1.inc";
        qreg q[6];
        creg meas[3];
        U(0.2, 0.5, 1.7) q[1];
        h q[0];
        h q[2];
        h q[4];
        h q[5];
        cx q[0], q[1];
        cx q[3], q[4];
    """
    if do_measure:
        qasm_program += '\nmeasure q -> meas;'

    circuit = circuit_from_qasm(qasm_program)

    # add some more gates
    q2 = cirq.NamedQubit('q_2')
    q3 = cirq.NamedQubit('q_3')
    circuit.insert(len(circuit) - 1, cirq.CXPowGate(exponent=0.723)(q2, q3))

    qubit_mapping = {
        'q_0': 'QB1',
        'q_1': 'QB2',
        'q_2': 'QB3',
        'q_3': 'QB4',
        'q_4': 'QB5',
        'q_5': 'QB6'
    }
    demo(device,
         circuit,
         do_measure,
         use_qsim=use_qsim,
         qubit_mapping=qubit_mapping)
示例#7
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())
示例#8
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())
示例#9
0
    # Canonicalizes exponent for equality, but keeps the inner details.
    assert cirq.Z**0.5 != cirq.Z**-0.5
    assert (cirq.Z**-1)**0.5 == cirq.Z**-0.5
    assert cirq.Z**-1 == cirq.Z


@pytest.mark.parametrize(
    'input_gate, specialized_output',
    [
        (cirq.Z, cirq.CZ),
        (cirq.CZ, cirq.CCZ),
        (cirq.X, cirq.CX),
        (cirq.CX, cirq.CCX),
        (cirq.ZPowGate(exponent=0.5), cirq.CZPowGate(exponent=0.5)),
        (cirq.CZPowGate(exponent=0.5), cirq.CCZPowGate(exponent=0.5)),
        (cirq.XPowGate(exponent=0.5), cirq.CXPowGate(exponent=0.5)),
        (cirq.CXPowGate(exponent=0.5), cirq.CCXPowGate(exponent=0.5)),
    ],
)
def test_specialized_control(input_gate, specialized_output):
    # Single qubit control on the input gate gives the specialized output
    assert input_gate.controlled() == specialized_output
    assert input_gate.controlled(num_controls=1) == specialized_output
    assert input_gate.controlled(
        control_values=((1, ), )) == specialized_output
    assert input_gate.controlled(control_qid_shape=(2, )) == specialized_output
    assert np.allclose(
        cirq.unitary(specialized_output),
        cirq.unitary(cirq.ControlledGate(input_gate, num_controls=1)),
    )
示例#10
0
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'),
)


@pytest.mark.parametrize('gate', VALID_GATES)
示例#11
0
 def _get_cx(self, q1, q2):
     if self.all_gates_parametrized:
         self.thetas += (sympy.symbols(f"theta{len(self.thetas)}"), )
         return cirq.CXPowGate(exponent=self.thetas[-1]).on(q1, q2)
     else:
         return cirq.CNOT(q1, q2)
示例#12
0
]

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),
]


class TestOperationValidation:
    """Nativity and validation of various operations."""
    @pytest.mark.parametrize('gate', native_1q_gates)
    @pytest.mark.parametrize('q', [0, 2, 3])
    def test_native_single_qubit_gates(self, adonis, gate, q):
        """Native operations must pass validation."""

        adonis.validate_operation(gate(adonis.qubits[q]))
        adonis.validate_operation(gate(adonis.qubits[q]).with_tags('tag_foo'))
示例#13
0
    z = cirq.ZPowGate(exponent=5)
    assert z.exponent == 5

    # Canonicalizes exponent for equality, but keeps the inner details.
    assert cirq.Z**0.5 != cirq.Z**-0.5
    assert (cirq.Z**-1)**0.5 == cirq.Z**-0.5
    assert cirq.Z**-1 == cirq.Z


@pytest.mark.parametrize(
    'input_gate, specialized_output',
    [(cirq.Z, cirq.CZ), (cirq.CZ, cirq.CCZ), (cirq.X, cirq.CX),
     (cirq.CX, cirq.CCX),
     (cirq.ZPowGate(exponent=0.5), cirq.CZPowGate(exponent=0.5)),
     (cirq.CZPowGate(exponent=0.5), cirq.CCZPowGate(exponent=0.5)),
     (cirq.XPowGate(exponent=0.5), cirq.CXPowGate(exponent=0.5)),
     (cirq.CXPowGate(exponent=0.5), cirq.CCXPowGate(exponent=0.5))])
def test_specialized_control(input_gate, specialized_output):
    # Single qubit control on the input gate gives the specialized output
    assert input_gate.controlled() == specialized_output
    assert input_gate.controlled(num_controls=1) == specialized_output
    assert input_gate.controlled(
        control_values=((1, ), )) == specialized_output
    assert input_gate.controlled(control_qid_shape=(2, )) == specialized_output
    assert np.allclose(
        cirq.unitary(specialized_output),
        cirq.unitary(cirq.ControlledGate(input_gate, num_controls=1)))

    # For multi-qudit controls, if the last control is a qubit with control
    # value 1, construct the specialized output leaving the rest of the
    # controls as they are.