Beispiel #1
0
def test_z_h_act_on_tableau():
    with pytest.raises(TypeError, match="Failed to act"):
        cirq.act_on(cirq.Z, DummyActOnArgs(), qubits=())
    with pytest.raises(TypeError, match="Failed to act"):
        cirq.act_on(cirq.H, DummyActOnArgs(), qubits=())
    original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
    flipped_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=23)

    args = cirq.ActOnCliffordTableauArgs(
        tableau=original_tableau.copy(),
        qubits=cirq.LineQubit.range(5),
        prng=np.random.RandomState(),
        log_of_measurement_results={},
    )

    cirq.act_on(cirq.H, args, [cirq.LineQubit(1)], allow_decompose=False)
    cirq.act_on(cirq.Z ** 0.5, args, [cirq.LineQubit(1)], allow_decompose=False)
    cirq.act_on(cirq.Z ** 0.5, args, [cirq.LineQubit(1)], allow_decompose=False)
    cirq.act_on(cirq.H, args, [cirq.LineQubit(1)], allow_decompose=False)
    assert args.log_of_measurement_results == {}
    assert args.tableau == flipped_tableau

    cirq.act_on(cirq.H, args, [cirq.LineQubit(1)], allow_decompose=False)
    cirq.act_on(cirq.Z, args, [cirq.LineQubit(1)], allow_decompose=False)
    cirq.act_on(cirq.H, args, [cirq.LineQubit(1)], allow_decompose=False)
    assert args.log_of_measurement_results == {}
    assert args.tableau == original_tableau

    cirq.act_on(cirq.H, args, [cirq.LineQubit(1)], allow_decompose=False)
    cirq.act_on(cirq.Z ** 3.5, args, [cirq.LineQubit(1)], allow_decompose=False)
    cirq.act_on(cirq.Z ** 3.5, args, [cirq.LineQubit(1)], allow_decompose=False)
    cirq.act_on(cirq.H, args, [cirq.LineQubit(1)], allow_decompose=False)
    assert args.log_of_measurement_results == {}
    assert args.tableau == flipped_tableau

    cirq.act_on(cirq.Z ** 2, args, [cirq.LineQubit(1)], allow_decompose=False)
    assert args.log_of_measurement_results == {}
    assert args.tableau == flipped_tableau

    cirq.act_on(cirq.H ** 2, args, [cirq.LineQubit(1)], allow_decompose=False)
    assert args.log_of_measurement_results == {}
    assert args.tableau == flipped_tableau

    foo = sympy.Symbol('foo')
    with pytest.raises(TypeError, match="Failed to act action on state"):
        cirq.act_on(cirq.Z ** foo, args, [cirq.LineQubit(1)])

    with pytest.raises(TypeError, match="Failed to act action on state"):
        cirq.act_on(cirq.H ** foo, args, [cirq.LineQubit(1)])

    with pytest.raises(TypeError, match="Failed to act action on state"):
        cirq.act_on(cirq.H ** 1.5, args, [cirq.LineQubit(1)])
Beispiel #2
0
def test_unsupported_stabilizer_safety():
    from cirq.protocols.act_on_protocol_test import DummyActOnArgs

    with pytest.raises(TypeError, match="act_on"):
        for _ in range(100):
            cirq.act_on(cirq.X.with_probability(0.5), DummyActOnArgs(), qubits=())
    with pytest.raises(TypeError, match="act_on"):
        cirq.act_on(cirq.X.with_probability(sympy.Symbol('x')), DummyActOnArgs(), qubits=())

    q = cirq.LineQubit(0)
    c = cirq.Circuit((cirq.X(q) ** 0.25).with_probability(0.5), cirq.measure(q, key='m'))
    with pytest.raises(TypeError, match='Failed to act'):
        cirq.StabilizerSampler().sample(c, repetitions=100)
Beispiel #3
0
def test_tagged_act_on():
    class YesActOn(cirq.Gate):
        def _num_qubits_(self) -> int:
            return 1

        def _act_on_(self, args, qubits):
            return True

    class NoActOn(cirq.Gate):
        def _num_qubits_(self) -> int:
            return 1

        def _act_on_(self, args, qubits):
            return NotImplemented

    class MissingActOn(cirq.Operation):
        def with_qubits(self, *new_qubits):
            raise NotImplementedError()

        @property
        def qubits(self):
            pass

    q = cirq.LineQubit(1)
    from cirq.protocols.act_on_protocol_test import DummyActOnArgs

    args = DummyActOnArgs()
    cirq.act_on(YesActOn()(q).with_tags("test"), args)
    with pytest.raises(TypeError, match="Failed to act"):
        cirq.act_on(NoActOn()(q).with_tags("test"), args)
    with pytest.raises(TypeError, match="Failed to act"):
        cirq.act_on(MissingActOn().with_tags("test"), args)
Beispiel #4
0
def test_reset_act_on():
    with pytest.raises(TypeError, match="Failed to act"):
        cirq.act_on(cirq.ResetChannel(), DummyActOnArgs(), qubits=())

    args = cirq.ActOnStateVectorArgs(
        available_buffer=np.empty(shape=(2, 2, 2, 2, 2), dtype=np.complex64),
        qubits=cirq.LineQubit.range(5),
        prng=np.random.RandomState(),
        log_of_measurement_results={},
        initial_state=cirq.one_hot(index=(1, 1, 1, 1, 1),
                                   shape=(2, 2, 2, 2, 2),
                                   dtype=np.complex64),
        dtype=np.complex64,
    )

    cirq.act_on(cirq.ResetChannel(), args, [cirq.LineQubit(1)])
    assert args.log_of_measurement_results == {}
    np.testing.assert_allclose(
        args.target_tensor,
        cirq.one_hot(index=(1, 0, 1, 1, 1),
                     shape=(2, 2, 2, 2, 2),
                     dtype=np.complex64),
    )

    cirq.act_on(cirq.ResetChannel(), args, [cirq.LineQubit(1)])
    assert args.log_of_measurement_results == {}
    np.testing.assert_allclose(
        args.target_tensor,
        cirq.one_hot(index=(1, 0, 1, 1, 1),
                     shape=(2, 2, 2, 2, 2),
                     dtype=np.complex64),
    )
Beispiel #5
0
def test_stabilizer_supports_depolarize():
    with pytest.raises(TypeError, match="act_on"):
        for _ in range(100):
            cirq.act_on(cirq.depolarize(3 / 4), DummyActOnArgs(), qubits=())

    q = cirq.LineQubit(0)
    c = cirq.Circuit(cirq.depolarize(3 / 4).on(q), cirq.measure(q, key='m'))
    m = np.sum(cirq.StabilizerSampler().sample(c, repetitions=100)['m'])
    assert 5 < m < 95
Beispiel #6
0
def test_cz_act_on_tableau():
    with pytest.raises(TypeError, match="Failed to act"):
        cirq.act_on(cirq.CZ, DummyActOnArgs(), qubits=())
    original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)

    args = cirq.ActOnCliffordTableauArgs(
        tableau=original_tableau.copy(),
        qubits=cirq.LineQubit.range(5),
        prng=np.random.RandomState(),
        log_of_measurement_results={},
    )

    cirq.act_on(cirq.CZ, args, cirq.LineQubit.range(2), allow_decompose=False)
    assert args.log_of_measurement_results == {}
    assert args.tableau.stabilizers() == [
        cirq.DensePauliString('ZIIII', coefficient=-1),
        cirq.DensePauliString('IZIII', coefficient=-1),
        cirq.DensePauliString('IIZII', coefficient=-1),
        cirq.DensePauliString('IIIZI', coefficient=-1),
        cirq.DensePauliString('IIIIZ', coefficient=-1),
    ]
    assert args.tableau.destabilizers() == [
        cirq.DensePauliString('XZIII', coefficient=1),
        cirq.DensePauliString('ZXIII', coefficient=1),
        cirq.DensePauliString('IIXII', coefficient=1),
        cirq.DensePauliString('IIIXI', coefficient=1),
        cirq.DensePauliString('IIIIX', coefficient=1),
    ]

    cirq.act_on(cirq.CZ, args, cirq.LineQubit.range(2), allow_decompose=False)
    assert args.log_of_measurement_results == {}
    assert args.tableau == original_tableau

    cirq.act_on(cirq.CZ**4,
                args,
                cirq.LineQubit.range(2),
                allow_decompose=False)
    assert args.log_of_measurement_results == {}
    assert args.tableau == original_tableau

    foo = sympy.Symbol('foo')
    with pytest.raises(TypeError, match="Failed to act action on state"):
        cirq.act_on(cirq.CZ**foo, args, cirq.LineQubit.range(2))

    with pytest.raises(TypeError, match="Failed to act action on state"):
        cirq.act_on(cirq.CZ**1.5, args, cirq.LineQubit.range(2))
Beispiel #7
0
def test_clifford_gate_act_on_fail():
    with pytest.raises(TypeError, match="Failed to act"):
        cirq.act_on(cirq.CliffordGate.X, DummyActOnArgs(), qubits=())