Esempio n. 1
0
def test_measure_state_no_indices_out_is_state():
    initial_state = cirq.to_valid_state_vector(0, 3)
    bits, state = cirq.measure_state_vector(initial_state, [],
                                            out=initial_state)
    assert [] == bits
    np.testing.assert_almost_equal(state, initial_state)
    assert state is initial_state
Esempio n. 2
0
def test_measure_state_partial_indices():
    for index in range(3):
        for x in range(8):
            initial_state = cirq.to_valid_state_vector(x, 3)
            bits, state = cirq.measure_state_vector(initial_state, [index])
            np.testing.assert_almost_equal(state, initial_state)
            assert bits == [bool(1 & (x >> (2 - index)))]
Esempio n. 3
0
def test_measure_state_partial_indices_all_orders():
    for perm in itertools.permutations([0, 1, 2]):
        for x in range(8):
            initial_state = cirq.to_valid_state_vector(x, 3)
            bits, state = cirq.measure_state_vector(initial_state, perm)
            np.testing.assert_almost_equal(state, initial_state)
            assert bits == [bool(1 & (x >> (2 - p))) for p in perm]
Esempio n. 4
0
def test_deprecated():
    with cirq.testing.assert_logs('cirq.bloch_vector_from_state_vector',
                                  'deprecated'):
        _ = cirq.sim.bloch_vector_from_state_vector(np.array([1, 0]), 0)

    with cirq.testing.assert_logs('cirq.density_matrix_from_state_vector',
                                  'deprecated'):
        _ = cirq.sim.density_matrix_from_state_vector(np.array([1, 0]))

    with cirq.testing.assert_logs('cirq.dirac_notation', 'deprecated'):
        _ = cirq.sim.dirac_notation(np.array([1, 0]))

    with cirq.testing.assert_logs('cirq.to_valid_state_vector', 'deprecated'):
        _ = cirq.sim.to_valid_state_vector(0, 1)

    with cirq.testing.assert_logs('irq.validate_normalized_state',
                                  'deprecated'):
        _ = cirq.sim.validate_normalized_state(np.array([1, 0],
                                                        dtype=np.complex64),
                                               qid_shape=(2, ))

    with cirq.testing.assert_logs('cirq.STATE_VECTOR_LIKE', 'deprecated'):
        # Reason for type: ignore: https://github.com/python/mypy/issues/5354
        _ = cirq.sim.STATE_VECTOR_LIKE  # type: ignore

    state_vector = np.array([1, 1]) / np.sqrt(2)
    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.sample_state_vector(state=state_vector, indices=[0])

    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.measure_state_vector(state=state_vector, indices=[0])
Esempio n. 5
0
def test_measure_state_no_indices_out_is_not_state():
    initial_state = cirq.to_valid_state_vector(0, 3)
    out = np.zeros_like(initial_state)
    bits, state = cirq.measure_state_vector(initial_state, [], out=out)
    assert [] == bits
    np.testing.assert_almost_equal(state, initial_state)
    assert state is out
    assert out is not initial_state
Esempio n. 6
0
def test_measure_state_out_is_not_state():
    initial_state = np.zeros(8, dtype=np.complex64)
    initial_state[0] = 1 / np.sqrt(2)
    initial_state[2] = 1 / np.sqrt(2)
    out = np.zeros_like(initial_state)
    _, state = cirq.measure_state_vector(initial_state, [2, 1, 0], out=out)
    assert out is not initial_state
    assert out is state
Esempio n. 7
0
def test_measure_state_seed():
    n = 10
    initial_state = np.ones(2**n) / 2**(n / 2)

    bits, state1 = cirq.measure_state_vector(initial_state, range(n), seed=1234)
    np.testing.assert_equal(
        bits,
        [False, False, True, True, False, False, False, True, False, False])

    bits, state2 = cirq.measure_state_vector(initial_state,
                                             range(n),
                                             seed=np.random.RandomState(1234))
    np.testing.assert_equal(
        bits,
        [False, False, True, True, False, False, False, True, False, False])

    np.testing.assert_allclose(state1, state2)
Esempio n. 8
0
def test_measure_state_out_is_state():
    initial_state = np.zeros(8, dtype=np.complex64)
    initial_state[0] = 1 / np.sqrt(2)
    initial_state[2] = 1 / np.sqrt(2)
    bits, state = cirq.measure_state_vector(initial_state, [2, 1, 0], out=initial_state)
    expected = np.zeros(8, dtype=np.complex64)
    expected[2 if bits[1] else 0] = 1.0
    np.testing.assert_array_almost_equal(initial_state, expected)
    assert state is initial_state
Esempio n. 9
0
def test_measure_state_reshape():
    results = []
    for x in range(8):
        initial_state = np.reshape(cirq.to_valid_state_vector(x, 3), [2] * 3)
        bits, state = cirq.measure_state_vector(initial_state, [2, 1, 0])
        results.append(bits)
        np.testing.assert_almost_equal(state, initial_state)
    expected = [list(reversed(x)) for x in list(itertools.product([False, True], repeat=3))]
    assert results == expected
Esempio n. 10
0
def test_measure_state_collapse():
    initial_state = np.zeros(8, dtype=np.complex64)
    initial_state[0] = 1 / np.sqrt(2)
    initial_state[2] = 1 / np.sqrt(2)
    for _ in range(10):
        bits, state = cirq.measure_state_vector(initial_state, [2, 1, 0])
        assert bits in [[False, False, False], [False, True, False]]
        expected = np.zeros(8, dtype=np.complex64)
        expected[2 if bits[1] else 0] = 1.0
        np.testing.assert_almost_equal(state, expected)
        assert state is not initial_state

    # Partial sample is correct.
    for _ in range(10):
        bits, state = cirq.measure_state_vector(initial_state, [2])
        np.testing.assert_almost_equal(state, initial_state)
        assert bits == [False]

        bits, state = cirq.measure_state_vector(initial_state, [0])
        np.testing.assert_almost_equal(state, initial_state)
        assert bits == [False]
Esempio n. 11
0
def test_measure_state_empty_state():
    initial_state = np.array([1.0])
    bits, state = cirq.measure_state_vector(initial_state, [])
    assert [] == bits
    np.testing.assert_almost_equal(state, initial_state)
Esempio n. 12
0
def test_measure_state_index_out_of_range():
    state = cirq.to_valid_state_vector(0, 3)
    with pytest.raises(IndexError, match='-2'):
        cirq.measure_state_vector(state, [-2])
    with pytest.raises(IndexError, match='3'):
        cirq.measure_state_vector(state, [3])
Esempio n. 13
0
def test_measure_state_not_power_of_two():
    with pytest.raises(ValueError, match='3'):
        _, _ = cirq.measure_state_vector(np.array([1, 0, 0]), [1])
    with pytest.raises(ValueError, match='5'):
        cirq.measure_state_vector(np.array([0, 1, 0, 0, 0]), [1])
Esempio n. 14
0
def test_measure_state_partial_indices_order():
    for x in range(8):
        initial_state = cirq.to_valid_state_vector(x, 3)
        bits, state = cirq.measure_state_vector(initial_state, [2, 1])
        np.testing.assert_almost_equal(state, initial_state)
        assert bits == [bool(1 & (x >> 0)), bool(1 & (x >> 1))]