예제 #1
0
def test_gate_equality():
    eq = cirq.testing.EqualsTester()
    eq.add_equality_group(cirq.CSwapGate(), cirq.CSwapGate())
    eq.add_equality_group(cirq.CZPowGate(), cirq.CZPowGate())
    eq.add_equality_group(cirq.CCXPowGate(), cirq.CCXPowGate(),
                          cirq.CCNotPowGate())
    eq.add_equality_group(cirq.CCZPowGate(), cirq.CCZPowGate())
예제 #2
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 CCZPowGate from controlled CZPowGate when applicable.

        This method is a specialized controlled method for CZPowGate. It
        overrides the default behavior of returning a ControlledGate by
        transforming the underlying controlled gate to a CCZPowGate 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 CCZPowGate is, by definition, a controlled-CZPowGate.)
        """
        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.CCZPowGate(exponent=self._exponent,
                                   global_shift=self._global_shift).controlled(
                                       result.num_controls() - 1,
                                       result.control_values[:-1],
                                       result.control_qid_shape[:-1])
        return result
예제 #3
0
 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
예제 #4
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 `CZPowGate`, using a `CCZPowGate` 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 `CCZPowGate` or a `ControlledGate` of a `CCZPowGate`, when
        this is possible.

        The conditions for the override to occur are:
            * The `global_shift` of the `CZPowGate` is 0.
            * The `control_values` and `control_qid_shape` are compatible with
                the `CCZPowGate`:
                * 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 `CCZPowGate`
        or, in the case that there is more than one controlled qudit, a
        `ControlledGate` with the `Gate` being a `CCZPowGate`. 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
        `CCZPowGate`).

        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.CCZPowGate(exponent=self._exponent,
                                   global_shift=self._global_shift).controlled(
                                       result.num_controls() - 1,
                                       result.control_values[:-1],
                                       result.control_qid_shape[:-1])
        return result
예제 #5
0
def test_cirq_qsim_all_supported_gates():
    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.IdentityGate(4).on(q0, q1, q2, q3),
        ),
        cirq.Moment(
            cirq.CCZPowGate(exponent=0.7, global_shift=0.3)(q2, q0, q1),
        ),
        cirq.Moment(
            cirq.CCXPowGate(exponent=0.4, global_shift=0.6)(q3, q1, q0).controlled_by(
                q2, control_values=[0]
            ),
        ),
        cirq.Moment(
            cirq.rx(0.3)(q0),
            cirq.ry(0.5)(q1),
            cirq.rz(0.7)(q2),
            cirq.rx(0.9)(q3),
        ),
        cirq.Moment(
            cirq.TwoQubitDiagonalGate([0.1, 0.2, 0.3, 0.4])(q0, q1),
        ),
        cirq.Moment(
            cirq.ThreeQubitDiagonalGate([0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.2, 1.3])(
                q1, q2, q3
            ),
        ),
        cirq.Moment(
            cirq.CSwapGate()(q0, q3, q1),
        ),
        cirq.Moment(
            cirq.rz(0.6)(q0),
            cirq.rx(0.7)(q1),
            cirq.ry(0.8)(q2),
            cirq.rz(0.9)(q3),
        ),
        cirq.Moment(
            cirq.TOFFOLI(q3, q2, q0),
        ),
        cirq.Moment(
            cirq.FREDKIN(q1, q3, q2),
        ),
        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()
    )
예제 #6
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)),
    )
예제 #7
0
파일: json_test.py 프로젝트: c-poole/Cirq
 'AsymmetricDepolarizingChannel':
 cirq.AsymmetricDepolarizingChannel(0.1, 0.2, 0.3),
 'BitFlipChannel':
 cirq.BitFlipChannel(0.5),
 'Bristlecone':
 cirq.google.Bristlecone,
 'CCNOT':
 cirq.CCNOT,
 'CCX':
 cirq.CCX,
 'CCXPowGate':
 cirq.CCXPowGate(exponent=0.123, global_shift=0.456),
 'CCZ':
 cirq.CCZ,
 'CCZPowGate':
 cirq.CCZPowGate(exponent=0.123, global_shift=0.456),
 'CNOT':
 cirq.CNOT,
 'CNotPowGate':
 cirq.CNotPowGate(exponent=0.123, global_shift=0.456),
 'ControlledOperation':
 cirq.ControlledOperation(sub_operation=cirq.Y(cirq.NamedQubit('target')),
                          controls=cirq.LineQubit.range(2),
                          control_values=[0, 1]),
 'ControlledGate':
 cirq.ControlledGate(sub_gate=cirq.Y,
                     num_controls=2,
                     control_values=[0, 1],
                     control_qid_shape=(3, 2)),
 'CX':
 cirq.CX,