def test_decomposed_fallback():
    class Composite(cirq.Gate):
        def num_qubits(self) -> int:
            return 1

        def _decompose_(self, qubits):
            yield cirq.X(*qubits)

    qid_shape = (2, )
    tensor = cirq.to_valid_density_matrix(0,
                                          len(qid_shape),
                                          qid_shape=qid_shape,
                                          dtype=np.complex64)
    args = cirq.ActOnDensityMatrixArgs(
        target_tensor=tensor,
        available_buffer=[np.empty_like(tensor) for _ in range(3)],
        qubits=cirq.LineQubit.range(1),
        prng=np.random.RandomState(),
        log_of_measurement_results={},
        qid_shape=qid_shape,
    )

    cirq.act_on(Composite(), args, cirq.LineQubit.range(1))
    np.testing.assert_allclose(
        args.target_tensor,
        cirq.one_hot(index=(1, 1), shape=(2, 2), dtype=np.complex64))
Ejemplo n.º 2
0
def test_shallow_copy_buffers():
    args = cirq.ActOnDensityMatrixArgs(
        qubits=cirq.LineQubit.range(1),
        initial_state=0,
    )
    copy = args.copy(deep_copy_buffers=False)
    assert copy.available_buffer is args.available_buffer
def test_shallow_copy_buffers():
    qid_shape = (2, )
    tensor = cirq.to_valid_density_matrix(0,
                                          len(qid_shape),
                                          qid_shape=qid_shape,
                                          dtype=np.complex64)
    args = cirq.ActOnDensityMatrixArgs(target_tensor=tensor)
    copy = args.copy(deep_copy_buffers=False)
    assert copy.available_buffer is args.available_buffer
Ejemplo n.º 4
0
def test_positional_argument():
    qid_shape = (2,)
    tensor = cirq.to_valid_density_matrix(
        0, len(qid_shape), qid_shape=qid_shape, dtype=np.complex64
    )
    with cirq.testing.assert_deprecated(
        'specify all the arguments with keywords', deadline='v0.15'
    ):
        cirq.ActOnDensityMatrixArgs(tensor)
def test_default_parameter():
    qid_shape = (2, )
    tensor = cirq.to_valid_density_matrix(0,
                                          len(qid_shape),
                                          qid_shape=qid_shape,
                                          dtype=np.complex64)
    args = cirq.ActOnDensityMatrixArgs(target_tensor=tensor)
    assert len(args.available_buffer) == 3
    for buffer in args.available_buffer:
        assert buffer.shape == tensor.shape
        assert buffer.dtype == tensor.dtype
    assert args.qid_shape == qid_shape
Ejemplo n.º 6
0
def test_cannot_act():
    class NoDetails:
        pass

    args = cirq.ActOnDensityMatrixArgs(
        qubits=cirq.LineQubit.range(1),
        prng=np.random.RandomState(),
        log_of_measurement_results={},
        initial_state=0,
        dtype=np.complex64,
    )
    with pytest.raises(TypeError, match="Can't simulate operations"):
        cirq.act_on(NoDetails(), args, qubits=())
Ejemplo n.º 7
0
def test_with_qubits():
    original = cirq.ActOnDensityMatrixArgs(
        qubits=cirq.LineQubit.range(1),
        initial_state=1,
        dtype=np.complex64,
    )
    extened = original.with_qubits(cirq.LineQubit.range(1, 2))
    np.testing.assert_almost_equal(
        extened.target_tensor,
        cirq.density_matrix_kronecker_product(
            np.array([[0, 0], [0, 1]], dtype=np.complex64),
            np.array([[1, 0], [0, 0]], dtype=np.complex64),
        ),
    )
Ejemplo n.º 8
0
def test_default_parameter():
    qid_shape = (2,)
    tensor = cirq.to_valid_density_matrix(
        0, len(qid_shape), qid_shape=qid_shape, dtype=np.complex64
    )
    args = cirq.ActOnDensityMatrixArgs(
        qubits=cirq.LineQubit.range(1),
        initial_state=0,
    )
    np.testing.assert_almost_equal(args.target_tensor, tensor)
    assert len(args.available_buffer) == 3
    for buffer in args.available_buffer:
        assert buffer.shape == tensor.shape
        assert buffer.dtype == tensor.dtype
    assert args.qid_shape == qid_shape
def test_cannot_act():
    class NoDetails:
        pass

    qid_shape = (2, )
    tensor = cirq.to_valid_density_matrix(0,
                                          len(qid_shape),
                                          qid_shape=qid_shape,
                                          dtype=np.complex64)
    args = cirq.ActOnDensityMatrixArgs(
        target_tensor=tensor,
        available_buffer=[np.empty_like(tensor) for _ in range(3)],
        qubits=cirq.LineQubit.range(1),
        prng=np.random.RandomState(),
        log_of_measurement_results={},
        qid_shape=qid_shape,
    )
    with pytest.raises(TypeError, match="Can't simulate operations"):
        cirq.act_on(NoDetails(), args, qubits=())
Ejemplo n.º 10
0
def test_decomposed_fallback():
    class Composite(cirq.Gate):
        def num_qubits(self) -> int:
            return 1

        def _decompose_(self, qubits):
            yield cirq.X(*qubits)

    args = cirq.ActOnDensityMatrixArgs(
        qubits=cirq.LineQubit.range(1),
        prng=np.random.RandomState(),
        log_of_measurement_results={},
        initial_state=0,
        dtype=np.complex64,
    )

    cirq.act_on(Composite(), args, cirq.LineQubit.range(1))
    np.testing.assert_allclose(
        args.target_tensor, cirq.one_hot(index=(1, 1), shape=(2, 2), dtype=np.complex64)
    )
def test_default_parameter_error():
    tensor = np.ndarray(shape=(2, ))
    with pytest.raises(
            ValueError,
            match='The dimension of target_tensor is not divisible by 2'):
        cirq.ActOnDensityMatrixArgs(target_tensor=tensor)
Ejemplo n.º 12
0
def test_deprecated_warning_and_default_parameter_error():
    tensor = np.ndarray(shape=(2,))
    with cirq.testing.assert_deprecated('Use initial_state instead', deadline='v0.15'):
        with pytest.raises(ValueError, match='dimension of target_tensor is not divisible by 2'):
            cirq.ActOnDensityMatrixArgs(target_tensor=tensor)
Ejemplo n.º 13
0
def test_axes_deprecation():
    shape = (2, )
    state = cirq.to_valid_density_matrix(0,
                                         len(shape),
                                         qid_shape=shape,
                                         dtype=np.complex64)
    buf = []
    log = {}
    qids = tuple(cirq.LineQubit.range(1))
    rng = np.random.RandomState()

    # No kwargs
    with cirq.testing.assert_deprecated("axes", deadline="v0.13"):
        args = cirq.ActOnDensityMatrixArgs(state, buf, (1, ), shape, rng, log,
                                           qids)  # type: ignore
    with cirq.testing.assert_deprecated("axes", deadline="v0.13"):
        assert args.axes == (1, )
    assert args.target_tensor is state
    assert args.available_buffer is buf
    assert args.qubits is qids
    assert args.qid_shape is shape
    assert args.prng is rng
    assert args.log_of_measurement_results is log

    # kwargs no axes
    with cirq.testing.assert_deprecated("axes", deadline="v0.13"):
        args = cirq.ActOnDensityMatrixArgs(
            state,
            buf,
            (1, ),  # type: ignore
            qubits=qids,
            prng=rng,
            log_of_measurement_results=log,
            qid_shape=shape,
        )
    with cirq.testing.assert_deprecated("axes", deadline="v0.13"):
        assert args.axes == (1, )
    assert args.target_tensor is state
    assert args.available_buffer is buf
    assert args.qubits is qids
    assert args.qid_shape is shape
    assert args.prng is rng
    assert args.log_of_measurement_results is log

    # kwargs incl axes
    with cirq.testing.assert_deprecated("axes", deadline="v0.13"):
        args = cirq.ActOnDensityMatrixArgs(
            state,
            buf,
            axes=(1, ),
            qubits=qids,
            prng=rng,
            log_of_measurement_results=log,
            qid_shape=shape,
        )
    with cirq.testing.assert_deprecated("axes", deadline="v0.13"):
        assert args.axes == (1, )
    assert args.target_tensor is state
    assert args.available_buffer is buf
    assert args.qubits is qids
    assert args.qid_shape is shape
    assert args.prng is rng
    assert args.log_of_measurement_results is log

    # all kwargs
    with cirq.testing.assert_deprecated("axes", deadline="v0.13"):
        args = cirq.ActOnDensityMatrixArgs(
            target_tensor=state,
            available_buffer=buf,
            axes=(1, ),
            qubits=qids,
            prng=rng,
            log_of_measurement_results=log,
            qid_shape=shape,
        )
    with cirq.testing.assert_deprecated("axes", deadline="v0.13"):
        assert args.axes == (1, )
    assert args.target_tensor is state
    assert args.available_buffer is buf
    assert args.qubits is qids
    assert args.qid_shape is shape
    assert args.prng is rng
    assert args.log_of_measurement_results is log