예제 #1
0
def test_single_qubit_phase_by():
    x = cirq.SingleQubitMatrixGate(np.array([[0, 1], [1, 0]]))
    y = cirq.SingleQubitMatrixGate(np.array([[0, -1j], [1j, 0]]))
    z = cirq.SingleQubitMatrixGate(np.array([[1, 0], [0, -1]]))
    assert cirq.phase_by(x, 0.25, 0).approx_eq(y)
    assert cirq.phase_by(y, -0.25, 0).approx_eq(x)
    assert cirq.phase_by(z, 0.25, 0).approx_eq(z)
예제 #2
0
def test_phase_by():
    # Single qubit case.
    x = cirq.MatrixGate(cirq.unitary(cirq.X))
    y = cirq.phase_by(x, 0.25, 0)
    cirq.testing.assert_allclose_up_to_global_phase(cirq.unitary(y),
                                                    cirq.unitary(cirq.Y),
                                                    atol=1e-8)

    # Two qubit case. Commutes with control.
    cx = cirq.MatrixGate(cirq.unitary(cirq.X.controlled(1)))
    cx2 = cirq.phase_by(cx, 0.25, 0)
    cirq.testing.assert_allclose_up_to_global_phase(cirq.unitary(cx2),
                                                    cirq.unitary(cx),
                                                    atol=1e-8)

    # Two qubit case. Doesn't commute with target.
    cy = cirq.phase_by(cx, 0.25, 1)
    cirq.testing.assert_allclose_up_to_global_phase(cirq.unitary(cy),
                                                    cirq.unitary(
                                                        cirq.Y.controlled(1)),
                                                    atol=1e-8)

    m = cirq.MatrixGate(np.eye(3), qid_shape=[3])
    with pytest.raises(TypeError, match='returned NotImplemented'):
        _ = cirq.phase_by(m, 0.25, 0)
예제 #3
0
def test_phase():
    g1 = cirq.SingleQubitGate()
    g2 = cirq.S
    g3 = cirq.phase_by(g2, 1, 0)
    qreg = cirq.LineQubit.range(2)
    op1 = cirq.ParallelGateOperation(g1, qreg)
    op2 = cirq.ParallelGateOperation(g2, qreg)
    assert cirq.phase_by(op2, 1, 0) == cirq.ParallelGateOperation(g3, qreg)
    with pytest.raises(TypeError):
        cirq.phase_by(op1, 1, 0)
예제 #4
0
def test_phase_by():
    g = cirq.PhasedXPowGate(phase_exponent=0.25)
    g2 = cirq.phase_by(g, 0.25, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=0.75)

    g = cirq.PhasedXPowGate(phase_exponent=0)
    g2 = cirq.phase_by(g, 0.125, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=0.25)

    g = cirq.PhasedXPowGate(phase_exponent=0.5)
    g2 = cirq.phase_by(g, 0.125, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=0.75)
예제 #5
0
def test_phase_by():
    g = cirq.PhasedXPowGate(phase_exponent=0.25)
    g2 = cirq.phase_by(g, 0.25, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=0.75)
    cirq.testing.assert_phase_by_is_consistent_with_unitary(g)

    g = cirq.PhasedXPowGate(phase_exponent=0)
    g2 = cirq.phase_by(g, 0.125, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=0.25)
    cirq.testing.assert_phase_by_is_consistent_with_unitary(g)

    g = cirq.PhasedXPowGate(phase_exponent=0.5)
    g2 = cirq.phase_by(g, 0.125, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=0.75)
    cirq.testing.assert_phase_by_is_consistent_with_unitary(g)
예제 #6
0
def test_phase_by():
    g = cirq.PhasedXPowGate(phase_exponent=0.25)
    g2 = cirq.phase_by(g, 0.25, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=0.75)

    g = cirq.PhasedXPowGate(phase_exponent=0)
    g2 = cirq.phase_by(g, 0.125, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=0.25)

    g = cirq.PhasedXPowGate(phase_exponent=0.5)
    g2 = cirq.phase_by(g, 0.125, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=0.75)

    g = cirq.PhasedXPowGate(phase_exponent=0.5)
    g2 = cirq.phase_by(g, sympy.Symbol('b') + 1, 0)
    assert g2 == cirq.PhasedXPowGate(phase_exponent=2 * sympy.Symbol('b') + 2.5)
예제 #7
0
def test_phase_by():
    assert cirq.phase_by(
        cirq.ControlledGate(UnphasableGate), 0.25, 1, default=None) is None
    sub_gate = cirq.Y
    phased_sub_gate = cirq.phase_by(sub_gate, 0.25, 0)
    assert phased_sub_gate != sub_gate
    cg = cirq.ControlledGate(sub_gate)
    assert cirq.phase_by(cg, 0.25, 0) == cg
    assert cirq.phase_by(cg, 0.25, 1) != cg
    assert cirq.phase_by(cg, 0.25, 1) == cirq.ControlledGate(phased_sub_gate)
    # Test that the qubit_index arg gets decremented at each subgate step.
    ccg = cirq.ControlledGate(cg)
    assert cirq.phase_by(ccg, 0.25, 0) == ccg
    assert cirq.phase_by(ccg, 0.25, 1) == ccg
    assert cirq.phase_by(ccg, 0.25, 2) != ccg
    assert (cirq.phase_by(ccg, 0.25, 2) == cirq.ControlledGate(
        cirq.ControlledGate(phased_sub_gate)))
예제 #8
0
def test_phase_by_xy():
    assert cirq.phase_by(cirq.X, 0.25, 0) == cirq.Y
    assert cirq.phase_by(cirq.Y, 0.25, 0) == cirq.X

    assert cirq.phase_by(cirq.X**0.5, 0.25, 0) == cirq.Y**0.5
    assert cirq.phase_by(cirq.Y**0.5, 0.25, 0) == cirq.X**-0.5
    assert cirq.phase_by(cirq.X**-0.5, 0.25, 0) == cirq.Y**-0.5
    assert cirq.phase_by(cirq.Y**-0.5, 0.25, 0) == cirq.X**0.5
예제 #9
0
def test_phase_by():
    class NoMethod:
        pass

    class ReturnsNotImplemented:
        def _phase_by_(self, phase_turns, qubit_on):
            return NotImplemented

    class PhaseIsAddition:
        def __init__(self, num_qubits):
            self.phase = [0] * num_qubits
            self.num_qubits = num_qubits

        def _phase_by_(self, phase_turns, qubit_on):
            if qubit_on >= self.num_qubits:
                return self
            self.phase[qubit_on] += phase_turns
            return self

    n = NoMethod()
    rin = ReturnsNotImplemented()

    # Without default

    with pytest.raises(TypeError, match='no _phase_by_ method'):
        _ = cirq.phase_by(n, 1, 0)
    with pytest.raises(TypeError, match='returned NotImplemented'):
        _ = cirq.phase_by(rin, 1, 0)

    # With default
    assert cirq.phase_by(n, 1, 0, default=None) == None
    assert cirq.phase_by(rin, 1, 0, default=None) == None

    test = PhaseIsAddition(3)
    assert test.phase == [0, 0, 0]
    test = cirq.phase_by(test, 0.25, 0)
    assert test.phase == [0.25, 0, 0]
    test = cirq.phase_by(test, 0.25, 0)
    assert test.phase == [0.50, 0, 0]
    test = cirq.phase_by(test, 0.40, 1)
    assert test.phase == [0.50, 0.40, 0]
    test = cirq.phase_by(test, 0.40, 4)
    assert test.phase == [0.50, 0.40, 0]
예제 #10
0
def test_phase_by_xy():
    assert cirq.phase_by(cirq.X, 0.25, 0) == cirq.Y
    assert cirq.phase_by(cirq.Y, 0.25, 0) == cirq.X

    assert cirq.phase_by(cirq.X**0.5, 0.25, 0) == cirq.Y**0.5
    assert cirq.phase_by(cirq.Y**0.5, 0.25, 0) == cirq.X**-0.5
    assert cirq.phase_by(cirq.X**-0.5, 0.25, 0) == cirq.Y**-0.5
    assert cirq.phase_by(cirq.Y**-0.5, 0.25, 0) == cirq.X**0.5

    cirq.testing.assert_phase_by_is_consistent_with_unitary(cirq.X)
    cirq.testing.assert_phase_by_is_consistent_with_unitary(cirq.Y)
    cirq.testing.assert_phase_by_is_consistent_with_unitary(cirq.X**0.5)
    cirq.testing.assert_phase_by_is_consistent_with_unitary(cirq.Y**0.5)

    cirq.testing.assert_phase_by_is_consistent_with_unitary(cirq.Rx(1))
    cirq.testing.assert_phase_by_is_consistent_with_unitary(cirq.Ry(1))
    cirq.testing.assert_phase_by_is_consistent_with_unitary(cirq.Rz(1))
예제 #11
0
def test_two_qubit_phase_by():
    x = np.array([[0, 1], [1, 0]])
    y = np.array([[0, -1j], [1j, 0]])
    z = np.array([[1, 0], [0, -1]])

    xx = cirq.TwoQubitMatrixGate(np.kron(x, x))
    yx = cirq.TwoQubitMatrixGate(np.kron(x, y))
    xy = cirq.TwoQubitMatrixGate(np.kron(y, x))
    yy = cirq.TwoQubitMatrixGate(np.kron(y, y))
    assert cirq.phase_by(xx, 0.25, 0).approx_eq(yx)
    assert cirq.phase_by(xx, 0.25, 1).approx_eq(xy)
    assert cirq.phase_by(xy, 0.25, 0).approx_eq(yy)
    assert cirq.phase_by(xy, -0.25, 1).approx_eq(xx)

    zz = cirq.TwoQubitMatrixGate(np.kron(z, z))
    assert cirq.phase_by(zz, 0.25, 0).approx_eq(zz)
    assert cirq.phase_by(zz, 0.25, 1).approx_eq(zz)
예제 #12
0
def test_tagged_operation_forwards_protocols():
    """The results of all protocols applied to an operation with a tag should
    be equivalent to the result without tags.
    """
    q1 = cirq.GridQubit(1, 1)
    q2 = cirq.GridQubit(1, 2)
    h = cirq.H(q1)
    tag = 'tag1'
    tagged_h = cirq.H(q1).with_tags(tag)

    np.testing.assert_equal(cirq.unitary(tagged_h), cirq.unitary(h))
    assert cirq.has_unitary(tagged_h)
    assert cirq.decompose(tagged_h) == cirq.decompose(h)
    assert cirq.pauli_expansion(tagged_h) == cirq.pauli_expansion(h)
    assert cirq.equal_up_to_global_phase(h, tagged_h)
    assert np.isclose(cirq.channel(h), cirq.channel(tagged_h)).all()

    assert cirq.measurement_key(cirq.measure(q1, key='blah').with_tags(tag)) == 'blah'

    parameterized_op = cirq.XPowGate(exponent=sympy.Symbol('t'))(q1).with_tags(tag)
    assert cirq.is_parameterized(parameterized_op)
    resolver = cirq.study.ParamResolver({'t': 0.25})
    assert cirq.resolve_parameters(parameterized_op, resolver) == cirq.XPowGate(exponent=0.25)(
        q1
    ).with_tags(tag)
    assert cirq.resolve_parameters_once(parameterized_op, resolver) == cirq.XPowGate(exponent=0.25)(
        q1
    ).with_tags(tag)

    y = cirq.Y(q1)
    tagged_y = cirq.Y(q1).with_tags(tag)
    assert tagged_y ** 0.5 == cirq.YPowGate(exponent=0.5)(q1)
    assert tagged_y * 2 == (y * 2)
    assert 3 * tagged_y == (3 * y)
    assert cirq.phase_by(y, 0.125, 0) == cirq.phase_by(tagged_y, 0.125, 0)
    controlled_y = tagged_y.controlled_by(q2)
    assert controlled_y.qubits == (
        q2,
        q1,
    )
    assert isinstance(controlled_y, cirq.Operation)
    assert not isinstance(controlled_y, cirq.TaggedOperation)

    clifford_x = cirq.SingleQubitCliffordGate.X(q1)
    tagged_x = cirq.SingleQubitCliffordGate.X(q1).with_tags(tag)
    assert cirq.commutes(clifford_x, clifford_x)
    assert cirq.commutes(tagged_x, clifford_x)
    assert cirq.commutes(clifford_x, tagged_x)
    assert cirq.commutes(tagged_x, tagged_x)

    assert cirq.trace_distance_bound(y ** 0.001) == cirq.trace_distance_bound(
        (y ** 0.001).with_tags(tag)
    )

    flip = cirq.bit_flip(0.5)(q1)
    tagged_flip = cirq.bit_flip(0.5)(q1).with_tags(tag)
    assert cirq.has_mixture(tagged_flip)
    assert cirq.has_channel(tagged_flip)

    flip_mixture = cirq.mixture(flip)
    tagged_mixture = cirq.mixture(tagged_flip)
    assert len(tagged_mixture) == 2
    assert len(tagged_mixture[0]) == 2
    assert len(tagged_mixture[1]) == 2
    assert tagged_mixture[0][0] == flip_mixture[0][0]
    assert np.isclose(tagged_mixture[0][1], flip_mixture[0][1]).all()
    assert tagged_mixture[1][0] == flip_mixture[1][0]
    assert np.isclose(tagged_mixture[1][1], flip_mixture[1][1]).all()

    qubit_map = {q1: 'q1'}
    qasm_args = cirq.QasmArgs(qubit_id_map=qubit_map)
    assert cirq.qasm(h, args=qasm_args) == cirq.qasm(tagged_h, args=qasm_args)

    cirq.testing.assert_has_consistent_apply_unitary(tagged_h)
예제 #13
0
def test_z_extrapolate():
    assert cirq.ZPowGate(exponent=1)**0.5 == cirq.ZPowGate(exponent=0.5)
    assert cirq.Z**-0.25 == cirq.ZPowGate(exponent=1.75)
    assert cirq.phase_by(cirq.ZPowGate(exponent=0.5), 0.25,
                         0) == cirq.ZPowGate(exponent=0.5)
예제 #14
0
def test_z_extrapolate():
    assert cirq.RotZGate(half_turns=1)**0.5 == cirq.RotZGate(half_turns=0.5)
    assert cirq.Z**-0.25 == cirq.RotZGate(half_turns=1.75)
    assert cirq.phase_by(cirq.RotZGate(half_turns=0.5), 0.25,
                         0) == cirq.RotZGate(half_turns=0.5)