Beispiel #1
0
def test_two_qubit_diagonal_gate_quil_output():
    pyquil = pytest.importorskip("pyquil")
    pyquil_simulation_tools = pytest.importorskip("pyquil.simulation.tools")
    q0, q1 = _make_qubits(2)
    operations = [
        cirq.TwoQubitDiagonalGate([np.pi / 2, 0, 0, 0])(q0, q1),
        cirq.TwoQubitDiagonalGate([0, np.pi / 2, 0, 0])(q0, q1),
        cirq.TwoQubitDiagonalGate([0, 0, np.pi / 2, 0])(q0, q1),
        cirq.TwoQubitDiagonalGate([0, 0, 0, np.pi / 2])(q0, q1),
    ]
    output = cirq.QuilOutput(operations, (q0, q1))
    program = pyquil.Program(str(output))
    assert f"\n{program.out()}" == QUIL_CPHASES_PROGRAM

    pyquil_unitary = pyquil_simulation_tools.program_unitary(program,
                                                             n_qubits=2)
    # Qubit ordering differs between pyQuil and Cirq.
    cirq_unitary = cirq.Circuit(cirq.SWAP(q0, q1), operations,
                                cirq.SWAP(q0, q1)).unitary()
    assert np.allclose(pyquil_unitary, cirq_unitary)
    # Also test non-CPHASE case.
    operations = [
        cirq.TwoQubitDiagonalGate([0, 0, 0, 0])(q0, q1),
    ]
    output = cirq.QuilOutput(operations, (q0, q1))
    program = pyquil.Program(str(output))
    assert f"\n{program.out()}" == QUIL_DIAGONAL_DEFGATE_PROGRAM
def test_two_qubit_diagonal_gate_quil_output():
    pyquil = pytest.importorskip("pyquil")
    pyquil_simulation_tools = pytest.importorskip("pyquil.simulation.tools")
    q0, q1 = _make_qubits(2)
    operations = [
        cirq.TwoQubitDiagonalGate([np.pi / 2, 0, 0, 0])(q0, q1),
        cirq.TwoQubitDiagonalGate([0, np.pi / 2, 0, 0])(q0, q1),
        cirq.TwoQubitDiagonalGate([0, 0, np.pi / 2, 0])(q0, q1),
        cirq.TwoQubitDiagonalGate([0, 0, 0, np.pi / 2])(q0, q1),
    ]
    with cirq.testing.assert_deprecated(deadline='v1.0', count=54):
        output = cirq.QuilOutput(operations, (q0, q1))
        program = pyquil.Program(str(output))
        assert f"\n{program.out()}" == QUIL_CPHASES_PROGRAM

        pyquil_unitary = pyquil_simulation_tools.program_unitary(program,
                                                                 n_qubits=2)
        # Qubit ordering differs between pyQuil and Cirq.
        cirq_unitary = cirq.Circuit(cirq.SWAP(q0, q1), operations,
                                    cirq.SWAP(q0, q1)).unitary()
        assert np.allclose(pyquil_unitary, cirq_unitary)
        # Also test non-CPHASE case, which decomposes into X/RZ/CPhase
        operations = [cirq.TwoQubitDiagonalGate([0, 0, 0, 0])(q0, q1)]
        output = cirq.QuilOutput(operations, (q0, q1))
        program = pyquil.Program(str(output))
        assert f"\n{program.out()}" == QUIL_DIAGONAL_DECOMPOSE_PROGRAM
def test_unitary():
    diagonal_angles = [2, 3, 5, 7]
    assert cirq.has_unitary(cirq.TwoQubitDiagonalGate(diagonal_angles))
    np.testing.assert_allclose(
        cirq.unitary(cirq.TwoQubitDiagonalGate(diagonal_angles)),
        np.diag([np.exp(1j * angle) for angle in diagonal_angles]),
        atol=1e-8)
Beispiel #4
0
def test_diagonal_exponent():
    diagonal_angles = [2, 3, 5, 7]
    diagonal_gate = cirq.TwoQubitDiagonalGate(diagonal_angles)

    sqrt_diagonal_gate = diagonal_gate ** 0.5

    expected_angles = [prime / 2 for prime in diagonal_angles]
    assert cirq.approx_eq(sqrt_diagonal_gate, cirq.TwoQubitDiagonalGate(expected_angles))

    assert cirq.pow(cirq.TwoQubitDiagonalGate(diagonal_angles), "test", None) is None
Beispiel #5
0
def test_resolve(resolve_fn):
    diagonal_angles = [2, 3, 5, 7]
    diagonal_gate = cirq.TwoQubitDiagonalGate(
        diagonal_angles[:2] + [sympy.Symbol('a'), sympy.Symbol('b')]
    )
    assert cirq.is_parameterized(diagonal_gate)

    diagonal_gate = resolve_fn(diagonal_gate, {'a': 5})
    assert diagonal_gate == cirq.TwoQubitDiagonalGate(diagonal_angles[:3] + [sympy.Symbol('b')])
    assert cirq.is_parameterized(diagonal_gate)

    diagonal_gate = resolve_fn(diagonal_gate, {'b': 7})
    assert diagonal_gate == cirq.TwoQubitDiagonalGate(diagonal_angles)
    assert not cirq.is_parameterized(diagonal_gate)
def test_parameterized_decompose():
    angles = sympy.symbols('x0, x1, x2, x3')
    parameterized_op = cirq.TwoQubitDiagonalGate(angles).on(
        *cirq.LineQubit.range(2))
    decomposed_circuit = cirq.Circuit(cirq.decompose(parameterized_op))
    for resolver in (cirq.Linspace('x0', -2, 2, 3) *
                     cirq.Linspace('x1', -2, 2, 3) *
                     cirq.Linspace('x2', -2, 2, 3) *
                     cirq.Linspace('x3', -2, 2, 3)):
        np.testing.assert_allclose(
            cirq.unitary(cirq.resolve_parameters(parameterized_op, resolver)),
            cirq.unitary(cirq.resolve_parameters(decomposed_circuit,
                                                 resolver)),
        )
def test_diagram():
    a, b = cirq.LineQubit.range(2)

    diagonal_circuit = cirq.Circuit(
        cirq.TwoQubitDiagonalGate([2, 3, 5, 7])(a, b))
    cirq.testing.assert_has_diagram(
        diagonal_circuit, """
0: ───diag(2, 3, 5, 7)───
      │
1: ───#2─────────────────
""")
    cirq.testing.assert_has_diagram(diagonal_circuit,
                                    """
0: ---diag(2, 3, 5, 7)---
      |
1: ---#2-----------------
""",
                                    use_unicode_characters=False)
def test_property():
    assert cirq.TwoQubitDiagonalGate([2, 3, 5,
                                      7]).diag_angles_radians == (2, 3, 5, 7)
def test_protocols_mul_not_implemented():
    diagonal_angles = [2, 3, None, 7]
    diagonal_gate = cirq.TwoQubitDiagonalGate(diagonal_angles)
    with pytest.raises(TypeError):
        cirq.protocols.pow(diagonal_gate, 3)
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 numpy as np
import pytest
import sympy

import cirq


@pytest.mark.parametrize(
    'gate',
    ((
        cirq.TwoQubitDiagonalGate([2, 3, 5, 7]),
        cirq.TwoQubitDiagonalGate([0, 0, 0, 0]),
        cirq.TwoQubitDiagonalGate([2, 3, 5, sympy.Symbol('a')]),
        cirq.TwoQubitDiagonalGate([0.34, 0.12, 0, 0.96]),
    )),
)
def test_consistent_protocols(gate):
    cirq.testing.assert_implements_consistent_protocols(gate)


def test_property():
    assert cirq.TwoQubitDiagonalGate([2, 3, 5,
                                      7]).diag_angles_radians == (2, 3, 5, 7)


def test_parameterized_decompose():
Beispiel #11
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()
    )