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

    state = cirq.CliffordTableauSimulationState(
        tableau=original_tableau.copy(),
        qubits=cirq.LineQubit.range(5),
        prng=np.random.RandomState(),
    )

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

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

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

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

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

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

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

    with pytest.raises(TypeError, match="Failed to act action on state"):
        cirq.act_on(cirq.H**1.5, state, [cirq.LineQubit(1)])
コード例 #2
0
def test_tagged_act_on():
    class YesActOn(cirq.Gate):
        def _num_qubits_(self) -> int:
            return 1

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

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

        def _act_on_(self, sim_state, 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 DummySimulationState

    args = DummySimulationState()
    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)
コード例 #3
0
def test_reset_act_on():
    with pytest.raises(TypeError, match="Failed to act"):
        cirq.act_on(cirq.ResetChannel(), DummySimulationState(), qubits=())

    args = cirq.StateVectorSimulationState(
        available_buffer=np.empty(shape=(2, 2, 2, 2, 2), dtype=np.complex64),
        qubits=cirq.LineQubit.range(5),
        prng=np.random.RandomState(),
        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),
    )
コード例 #4
0
def test_unsupported_stabilizer_safety():
    from cirq.protocols.act_on_protocol_test import DummySimulationState

    with pytest.raises(TypeError, match="act_on"):
        for _ in range(100):
            cirq.act_on(cirq.X.with_probability(0.5),
                        DummySimulationState(),
                        qubits=())
    with pytest.raises(TypeError, match="act_on"):
        cirq.act_on(cirq.X.with_probability(sympy.Symbol('x')),
                    DummySimulationState(),
                    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)
コード例 #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),
                        DummySimulationState(),
                        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
コード例 #6
0
def test_cz_act_on_tableau():
    with pytest.raises(TypeError, match="Failed to act"):
        cirq.act_on(cirq.CZ, DummySimulationState(), qubits=())
    original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)

    state = cirq.CliffordTableauSimulationState(
        tableau=original_tableau.copy(),
        qubits=cirq.LineQubit.range(5),
        prng=np.random.RandomState(),
    )

    cirq.act_on(cirq.CZ, state, cirq.LineQubit.range(2), allow_decompose=False)
    assert state.log_of_measurement_results == {}
    assert state.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 state.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, state, cirq.LineQubit.range(2), allow_decompose=False)
    assert state.log_of_measurement_results == {}
    assert state.tableau == original_tableau

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

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

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