forked from quantumlib/Cirq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for > 32 qudits to cirq.sample_state_vector. Fix for quan…
…tumlib#6031 (quantumlib#6090) * Add support for > 32 qudits to cirq.sample_state_vector. Fix for quantumlib#6031 * refactor the method into a seprate util module * fix lint * accept only probabilities not complex numbers * added tests
- Loading branch information
1 parent
7b143cc
commit a8e1d45
Showing
8 changed files
with
226 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2023 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Sequence, Tuple | ||
|
||
import numpy as np | ||
|
||
from cirq import linalg | ||
|
||
|
||
def state_probabilities_by_indices( | ||
state_probability: np.ndarray, indices: Sequence[int], qid_shape: Tuple[int, ...] | ||
) -> np.ndarray: | ||
"""Returns the probabilities for a state/measurement on the given indices. | ||
Args: | ||
state_probability: The multi-qubit state probability vector. This is an | ||
array of 2 to the power of the number of real numbers, and | ||
so state must be of size ``2**integer``. The `state_probability` can be | ||
a vector of size ``2**integer`` or a tensor of shape | ||
``(2, 2, ..., 2)``. | ||
indices: Which qubits are measured. The `state_probability` is assumed to be | ||
supplied in big endian order. That is the xth index of v, when | ||
expressed as a bitstring, has its largest values in the 0th index. | ||
qid_shape: The qid shape of the `state_probability`. | ||
Returns: | ||
State probabilities. | ||
""" | ||
probs = state_probability.reshape((-1,)) | ||
not_measured = [i for i in range(len(qid_shape)) if i not in indices] | ||
if linalg.can_numpy_support_shape(qid_shape): | ||
# Use numpy transpose if we can since it's more efficient. | ||
probs = probs.reshape(qid_shape) | ||
probs = np.transpose(probs, list(indices) + not_measured) | ||
probs = probs.reshape((-1,)) | ||
else: | ||
# If we can't use numpy due to numpy/numpy#5744, use a slower method. | ||
probs = linalg.transpose_flattened_array(probs, qid_shape, list(indices) + not_measured) | ||
|
||
if len(not_measured): | ||
# Not all qudits are measured. | ||
volume = np.prod([qid_shape[i] for i in indices]) | ||
# Reshape into a 2D array in which each of the measured states correspond to a row. | ||
probs = probs.reshape((volume, -1)) | ||
probs = np.sum(probs, axis=-1) | ||
|
||
# To deal with rounding issues, ensure that the probabilities sum to 1. | ||
return probs / np.sum(probs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright 2023 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import pytest | ||
|
||
import numpy as np | ||
|
||
from cirq.sim import simulation_utils | ||
from cirq import testing | ||
|
||
|
||
@pytest.mark.parametrize('n,m', [(n, m) for n in range(1, 4) for m in range(1, n + 1)]) | ||
def test_state_probabilities_by_indices(n: int, m: int): | ||
np.random.seed(0) | ||
state = testing.random_superposition(1 << n) | ||
d = (state.conj() * state).real | ||
desired_axes = list(np.random.choice(n, m, replace=False)) | ||
not_wanted = [i for i in range(n) if i not in desired_axes] | ||
got = simulation_utils.state_probabilities_by_indices(d, desired_axes, (2,) * n) | ||
want = np.transpose(d.reshape((2,) * n), desired_axes + not_wanted) | ||
want = np.sum(want.reshape((1 << len(desired_axes), -1)), axis=-1) | ||
np.testing.assert_allclose(want, got) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.