def test_quantum_state_product_state(): q0, q1, q2 = cirq.LineQubit.range(3) product_state_1 = cirq.KET_PLUS(q0) * cirq.KET_PLUS(q1) * cirq.KET_ONE(q2) state = cirq.quantum_state(product_state_1) np.testing.assert_allclose(state.data, product_state_1.state_vector()) assert state.qid_shape == (2, 2, 2) assert state.dtype == np.complex64 with pytest.raises(ValueError, match='qid shape'): _ = cirq.quantum_state(product_state_1, qid_shape=(2, 2))
def test_quantum_state_density_matrix(): density_matrix_1 = np.eye(4, dtype=np.complex64) / 4 state = cirq.quantum_state(density_matrix_1, qid_shape=(4,), copy=True) assert state.data is not density_matrix_1 np.testing.assert_array_equal(state.data, density_matrix_1) assert state.qid_shape == (4,) assert state.dtype == np.complex64 with pytest.raises(ValueError, match='not compatible'): _ = cirq.quantum_state(density_matrix_1, qid_shape=(8,))
def test_quantum_state_state_vector_state_tensor(): state_vector_1 = cirq.one_hot(shape=(4,), dtype=np.complex128) state_tensor_1 = np.reshape(state_vector_1, (2, 2)) state = cirq.quantum_state(state_vector_1, dtype=np.complex64, qid_shape=(2, 2)) np.testing.assert_array_equal(state.data, state_vector_1) assert state.qid_shape == (2, 2) assert state.dtype == np.complex64 state = cirq.quantum_state(state_tensor_1, qid_shape=(2, 2)) assert state.data is state_tensor_1 assert state.qid_shape == (2, 2) assert state.dtype == np.complex128
def test_von_neumann_entropy(): # 1x1 matrix assert cirq.von_neumann_entropy(np.array([[1]])) == 0 # An EPR pair state (|00> + |11>)(<00| + <11|) assert ( cirq.von_neumann_entropy(0.5 * np.array([1, 0, 0, 1] * np.array([[1], [0], [0], [1]]))) == 0 ) # Maximally mixed state # yapf: disable assert cirq.von_neumann_entropy(np.array( [[0.5, 0], [0, 0.5]])) == 1 # yapf: enable # 2x2 random unitary, each column as a ket, each ket as a density matrix, # linear combination of the two with coefficients 0.1 and 0.9 res = cirq.testing.random_unitary(2) first_column = res[:, 0] first_density_matrix = 0.1 * np.outer(first_column, np.conj(first_column)) second_column = res[:, 1] second_density_matrix = 0.9 * np.outer(second_column, np.conj(second_column)) assert np.isclose( cirq.von_neumann_entropy(first_density_matrix + second_density_matrix), 0.4689, atol=1e-04 ) assert np.isclose( cirq.von_neumann_entropy(np.diag([0, 0, 0.1, 0, 0.2, 0.3, 0.4, 0])), 1.8464, atol=1e-04 ) # Random NxN matrix probs = np.random.exponential(size=N) probs /= np.sum(probs) mat = U @ (probs * U).T.conj() np.testing.assert_allclose( cirq.von_neumann_entropy(mat), -np.sum(probs * np.log(probs) / np.log(2)) ) # QuantumState object assert ( cirq.von_neumann_entropy(cirq.quantum_state(np.array([[0.5, 0], [0, 0.5]]), qid_shape=(2,))) == 1 ) assert ( cirq.von_neumann_entropy( cirq.quantum_state(np.array([[0.5, 0.5], [0.5, 0.5]]), qid_shape=(2, 2)) ) == 0 )
def test_quantum_state_computational_basis_state(): state = cirq.quantum_state(7, qid_shape=(3, 4)) np.testing.assert_allclose( state.data, cirq.one_hot(index=7, shape=(12, ), dtype=np.complex64)) assert state.qid_shape == (3, 4) assert state.dtype == np.complex64 state = cirq.quantum_state((0, 1, 2, 3), qid_shape=(1, 2, 3, 4), dtype=np.complex128) np.testing.assert_allclose( state.data, cirq.one_hot(index=(0, 1, 2, 3), shape=(1, 2, 3, 4), dtype=np.complex64)) assert state.qid_shape == (1, 2, 3, 4) assert state.dtype == np.complex128 with pytest.raises(ValueError, match='ambiguous'): _ = cirq.quantum_state(7) with pytest.raises(ValueError, match='out of range'): _ = cirq.quantum_state(7, qid_shape=(2, 2)) with pytest.raises(ValueError, match='ambiguous'): _ = cirq.quantum_state((0, 1, 2, 3)) with pytest.raises(ValueError, match='out of bounds'): _ = cirq.quantum_state((0, 1, 2, 3), qid_shape=(2, 2, 2, 2)) with pytest.raises(ValueError, match='ambiguous'): _ = cirq.quantum_state((0, 0, 1, 1), qid_shape=(1, 1, 2, 2))
def test_quantum_state_state_vector_state_tensor(): state_vector_1 = cirq.one_hot(shape=(4, ), dtype=np.complex128) state_tensor_1 = np.reshape(state_vector_1, (2, 2)) state = cirq.quantum_state(state_vector_1, dtype=np.complex64) np.testing.assert_array_equal(state.data, state_vector_1) assert state.qid_shape == (2, 2) assert state.dtype == np.complex64 state = cirq.quantum_state(state_tensor_1, qid_shape=(2, 2)) assert state.data is state_tensor_1 assert state.qid_shape == (2, 2) assert state.dtype == np.complex128 with pytest.raises(ValueError, match='ambiguous'): _ = cirq.quantum_state(state_tensor_1) with pytest.raises(ValueError, match='not compatible'): _ = cirq.quantum_state(state_tensor_1, qid_shape=(2, 3))
def test_quantum_state_quantum_state(): state_vector_1 = cirq.one_hot(shape=(4,), dtype=np.complex128) quantum_state = cirq.QuantumState(state_vector_1, qid_shape=(2, 2)) state = cirq.quantum_state(quantum_state) assert state is quantum_state assert state.data is quantum_state.data assert state.dtype == np.complex128 state = cirq.quantum_state(quantum_state, copy=True) assert state is not quantum_state assert state.data is not quantum_state.data assert state.dtype == np.complex128 state = cirq.quantum_state(quantum_state, dtype=np.complex64) assert state is not quantum_state assert state.data is not quantum_state.data assert state.dtype == np.complex64 with pytest.raises(ValueError, match='qid shape'): state = cirq.quantum_state(quantum_state, qid_shape=(4,))