Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cirq-core/cirq/ops/linear_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,14 +698,14 @@ def expectation_from_state_vector(

size = state_vector.size
num_qubits = size.bit_length() - 1
_validate_qubit_mapping(qubit_map, self.qubits, num_qubits)

if len(state_vector.shape) != 1 and state_vector.shape != (2,) * num_qubits:
if state_vector.shape not in ((2**num_qubits,), (2,) * num_qubits):
raise ValueError(
"Input array does not represent a state vector "
"with shape `(2 ** n,)` or `(2, ..., 2)`."
)

_validate_qubit_mapping(qubit_map, self.qubits, num_qubits)

if check_preconditions:
qis.validate_normalized_state_vector(
state_vector=state_vector,
Expand Down
11 changes: 8 additions & 3 deletions cirq-core/cirq/ops/linear_combinations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ def test_expectation_from_state_vector_invalid_input() -> None:
q0, q1, q2, q3 = cirq.LineQubit.range(4)
psum = cirq.X(q0) + 2 * cirq.Y(q1) + 3 * cirq.Z(q3)
q_map = {q0: 0, q1: 1, q3: 2}
wf: np.ndarray = np.array([1, 0, 0, 0, 0, 0, 0], dtype=np.complex64)
wf: np.ndarray = np.array([1, 0, 0, 0, 0, 0, 0, 0], dtype=np.complex64)

im_psum = (1j + 1) * psum
with pytest.raises(NotImplementedError, match='non-Hermitian'):
Expand All @@ -1333,8 +1333,13 @@ def test_expectation_from_state_vector_invalid_input() -> None:
with pytest.raises(ValueError, match='indices'):
psum.expectation_from_state_vector(wf, {q0: 0, q1: 0, q3: 2})

with pytest.raises(ValueError, match='9'):
psum.expectation_from_state_vector(np.arange(9, dtype=np.complex64), q_map)
for bad_size in (3, 5, 7, 9):
with pytest.raises(ValueError, match='shape'):
psum.expectation_from_state_vector(np.zeros(bad_size, dtype=np.complex64), q_map)
with pytest.raises(ValueError, match='shape'):
psum.expectation_from_state_vector(
np.zeros(bad_size, dtype=np.complex64), q_map, check_preconditions=False
)
q_map_2 = {q0: 0, q1: 1, q2: 2, q3: 3}
with pytest.raises(ValueError, match='normalized'):
psum.expectation_from_state_vector(np.arange(16, dtype=np.complex64), q_map_2)
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/pauli_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ def expectation_from_state_vector(

size = state_vector.size
num_qubits = size.bit_length() - 1
if len(state_vector.shape) != 1 and state_vector.shape != (2,) * num_qubits:
if state_vector.shape not in ((2**num_qubits,), (2,) * num_qubits):
raise ValueError(
"Input array does not represent a state vector "
"with shape `(2 ** n,)` or `(2, ..., 2)`."
Expand Down
9 changes: 7 additions & 2 deletions cirq-core/cirq/ops/pauli_string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,13 @@ def test_expectation_from_state_vector_invalid_input() -> None:
_ = ps.expectation_from_state_vector(wf, {q0: 0, q1: 1, q2: 0})

# Incorrectly shaped state_vector input.
with pytest.raises(ValueError, match='7'):
ps.expectation_from_state_vector(np.arange(7, dtype=np.complex64), q_map)
for bad_size in (3, 5, 7):
with pytest.raises(ValueError, match='shape'):
ps.expectation_from_state_vector(np.zeros(bad_size, dtype=np.complex64), q_map)
with pytest.raises(ValueError, match='shape'):
ps.expectation_from_state_vector(
np.zeros(bad_size, dtype=np.complex64), q_map, check_preconditions=False
)
q_map_2 = {q0: 0, q1: 1, q2: 2, q3: 3}
with pytest.raises(ValueError, match='normalized'):
ps.expectation_from_state_vector(np.arange(16, dtype=np.complex64), q_map_2)
Expand Down
Loading