-
Notifications
You must be signed in to change notification settings - Fork 975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More numpy types #5683
More numpy types #5683
Changes from 2 commits
4674a52
05db9cd
a32ab1d
7185741
a8ab8c2
fda0eb2
187732d
0a820e0
de5c3cc
e831edb
cce5515
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,6 +331,7 @@ def _trace_distance_bound_(self) -> Optional[float]: | |
if protocols.is_parameterized(self._exponent): | ||
return None | ||
angles = np.pi * (np.array(self._eigen_shifts()) * self._exponent % 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra debug print? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://en.wikipedia.org/wiki/D%27oh! Removed. |
||
print(type(angles)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. |
||
return protocols.trace_distance_from_angle_list(angles) | ||
|
||
def _has_unitary_(self) -> bool: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -644,7 +644,7 @@ def expectation_from_density_matrix( | |
*, | ||
atol: float = 1e-7, | ||
check_preconditions: bool = True, | ||
) -> float: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should remain float. The bug is that in numpy.trace below, which should just be unpacked into a float. line 746. Does that fix things? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've returned |
||
) -> np.ndarray: | ||
r"""Evaluate the expectation of this PauliString given a density matrix. | ||
|
||
Compute the expectation value of this PauliString with respect to an | ||
|
@@ -716,7 +716,7 @@ def expectation_from_density_matrix( | |
|
||
def _expectation_from_density_matrix_no_validation( | ||
self, state: np.ndarray, qubit_map: Mapping[TKey, int] | ||
) -> float: | ||
) -> np.ndarray: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return value of this function is scalar. After adding a debug printout the Is the expectation value guaranteed to be a float? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've converted it to a float. |
||
"""Evaluate the expectation of this PauliString given a density matrix. | ||
|
||
This method does not provide input validation. See | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,7 +19,7 @@ | |||||
from cirq import protocols | ||||||
from cirq._compat import proper_repr | ||||||
from cirq.qis import quantum_state_representation | ||||||
from cirq.value import big_endian_int_to_digits, linear_dict | ||||||
from cirq.value import big_endian_int_to_digits, linear_dict, random_state | ||||||
|
||||||
if TYPE_CHECKING: | ||||||
import cirq | ||||||
|
@@ -509,11 +509,14 @@ def destabilizers(self) -> List['cirq.DensePauliString']: | |||||
generators above generate the full Pauli group on n qubits.""" | ||||||
return [self._row_to_dense_pauli(i) for i in range(self.n)] | ||||||
|
||||||
def _measure(self, q, prng: np.random.RandomState = np.random) -> int: | ||||||
def _measure(self, q, prng: Optional[np.random.RandomState] = None) -> int: | ||||||
"""Performs a projective measurement on the q'th qubit. | ||||||
|
||||||
Returns: the result (0 or 1) of the measurement. | ||||||
""" | ||||||
real_prng: np.random.RandomState = ( | ||||||
random_state.parse_random_state(np.random) if prng is None else prng | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think this can be simplied to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. Applied. |
||||||
) | ||||||
is_commuting = True | ||||||
for i in range(self.n, 2 * self.n): | ||||||
if self.xs[i, q]: | ||||||
|
@@ -544,7 +547,7 @@ def _measure(self, q, prng: np.random.RandomState = np.random) -> int: | |||||
|
||||||
self.zs[p, q] = True | ||||||
|
||||||
self.rs[p] = bool(prng.randint(2)) | ||||||
self.rs[p] = bool(real_prng.randint(2)) | ||||||
|
||||||
return int(self.rs[p]) | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,12 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Any, Dict, List, Sequence, Union | ||
from typing import Any, Dict, List, Sequence, Optional, Union | ||
import numpy as np | ||
|
||
import cirq | ||
from cirq import protocols, qis, value | ||
from cirq.value import big_endian_int_to_digits | ||
from cirq.value import big_endian_int_to_digits, random_state | ||
|
||
|
||
@value.value_equality | ||
|
@@ -236,17 +236,20 @@ def to_state_vector(self) -> np.ndarray: | |
|
||
return arr | ||
|
||
def _measure(self, q, prng: np.random.RandomState) -> int: | ||
def _measure(self, q, prng: Optional[np.random.RandomState] = None) -> int: | ||
"""Measures the q'th qubit. | ||
|
||
Reference: Section 4.1 "Simulating measurements" | ||
|
||
Returns: Computational basis measurement as 0 or 1. | ||
""" | ||
real_prng: np.random.RandomState = ( | ||
random_state.parse_random_state(np.random) if prng is None else prng | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar simplification as before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done for this and |
||
) | ||
w = self.s.copy() | ||
for i, v_i in enumerate(self.v): | ||
if v_i == 1: | ||
w[i] = bool(prng.randint(2)) | ||
w[i] = bool(real_prng.randint(2)) | ||
x_i = sum(w & self.G[q, :]) % 2 | ||
# Project the state to the above measurement outcome. | ||
self.project_Z(q, x_i) | ||
|
@@ -386,7 +389,7 @@ def apply_global_phase(self, coefficient: value.Scalar): | |
self.omega *= coefficient | ||
|
||
def measure( | ||
self, axes: Sequence[int], seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None | ||
self, axes: Sequence[int], seed: Optional['cirq.RANDOM_STATE_OR_SEED_LIKE'] = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, keep the seed type as it was, RANDOM_STATE_OR_SEED_LIKE is typed as Any so None should be fine there. Add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The seed type is assigned to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't confirmed that VarType = Any
def hi(var: VarType=None) Fails with Anyway, will apply your suggestion since |
||
) -> List[int]: | ||
return [self._measure(axis, seed) for axis in axes] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,6 @@ def sample_density_matrix( | |
qid_shape = (2,) * num_qubits | ||
else: | ||
_validate_density_matrix_qid_shape(density_matrix, qid_shape) | ||
num_qubits = len(qid_shape) | ||
meas_shape = _indices_shape(qid_shape, indices) | ||
|
||
if repetitions == 0 or len(indices) == 0: | ||
|
@@ -139,15 +138,18 @@ def measure_density_matrix( | |
qid_shape = (2,) * num_qubits | ||
else: | ||
_validate_density_matrix_qid_shape(density_matrix, qid_shape) | ||
num_qubits = len(qid_shape) | ||
meas_shape = _indices_shape(qid_shape, indices) | ||
|
||
def _copy_density_matrix_to_out(density_matrix: np.ndarray, out: np.ndarray) -> np.ndarray: | ||
np.copyto(dst=out, src=density_matrix) | ||
return out | ||
|
||
arrout: np.ndarray = ( | ||
np.copy(density_matrix) | ||
if out is None | ||
else density_matrix | ||
if out is density_matrix | ||
else (np.copyto(dst=out, src=density_matrix), out)[-1] | ||
else _copy_density_matrix_to_out(density_matrix, out) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please consider replacing with hopefully more readable arrout: np.ndarray
if out is None:
arrout = np.copy(density_matrix)
elif out is density_matrix:
arrout = density_matrix
else:
np.copyto(dst=out, src=density_matrix)
arrout = out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
) | ||
|
||
if len(indices) == 0: | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -226,7 +226,7 @@ def prepare_into_buffer(k: int): | |||||
] | ||||||
p = prng.random() | ||||||
weight = None | ||||||
fallback_weight = 0 | ||||||
fallback_weight = np.float64(0) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get
|
||||||
fallback_weight_index = 0 | ||||||
index = None | ||||||
for index in range(len(kraus_tensors)): | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from typing import Union, Sequence, Optional | ||
|
||
import numpy as np | ||
from cirq.value import random_state | ||
|
||
_RealArraylike = Union[np.ndarray, float] | ||
|
||
|
@@ -58,7 +59,9 @@ def random_qubit_unitary( | |
rng: Random number generator to be used in sampling. Default is | ||
numpy.random. | ||
""" | ||
real_rng: np.random.RandomState = np.random if rng is None else rng | ||
real_rng: np.random.RandomState = ( | ||
random_state.parse_random_state(np.random) if rng is None else rng | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. simplify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please replace with a simple call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
theta = np.arcsin(np.sqrt(real_rng.rand(*shape))) | ||
phi_d = real_rng.rand(*shape) * np.pi * 2 | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -93,9 +93,12 @@ def plot_state_histogram( | |||||||||||||||||
tick_label, values = zip(*sorted(data.items())) | ||||||||||||||||||
else: | ||||||||||||||||||
values = np.array(data) | ||||||||||||||||||
if not tick_label: | ||||||||||||||||||
tick_label = np.arange(len(values)) | ||||||||||||||||||
ax.bar(np.arange(len(values)), values, tick_label=tick_label) | ||||||||||||||||||
values_range = np.arange(len(values)) | ||||||||||||||||||
_ = ( | ||||||||||||||||||
ax.bar(values_range, values, tick_label=values_range) | ||||||||||||||||||
if tick_label is None | ||||||||||||||||||
else ax.bar(values_range, values, tick_label=tick_label) | ||||||||||||||||||
) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mypy complaint was
Let's replace with
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||||||||||||||
ax.set_xlabel(xlabel) | ||||||||||||||||||
ax.set_ylabel(ylabel) | ||||||||||||||||||
ax.set_title(title) | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very strange to be using a numpy array to store Moments. I think for this one one needs to go back to the source where the numpy array is created and use a sequence instead (and change signature of concat_ragged function as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed
buffer = np.zeros(shape=pad_len * 2 + n_acc, dtype=object)
tobuffer: MutableSequence['cirq.Moment'] = [cirq.Moment()] * (pad_len * 2 + n_acc)
. Please take another look.