-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Density Matrix Simulator ActOn migration #3841
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
ddd2bfb
Start density matrix ActOn migration
daxfohl d4b6de3
Merge branch 'master' into acton
daxfohl 4a83834
Fix initial buffer tensors
daxfohl 608f893
Allow decomposition
daxfohl 8a0b6a5
Fix qid shape for act_on args
daxfohl fe35308
Oops, left out the noise
daxfohl 97e1970
Fix terminal measurement check
daxfohl fd0effc
Ensure noise op tree is flattened
daxfohl ba66b99
rename vars
daxfohl 89c9331
rename vars
daxfohl fe31298
Merge branch 'master' into acton
daxfohl 0ad0a46
Create base class for ActOnArgs
daxfohl 0d9d698
Create base class for ActOnArgs
daxfohl 25116e2
Create base class for ActOnArgs
daxfohl e387f6e
Merge branch 'master' into acton
daxfohl 7ce9dd7
Merge branch 'master' into acton
daxfohl 93022f6
Merge branch 'master' into acton
daxfohl 218fcdd
Merge branch 'master' into acton
daxfohl d17d076
Merge branch 'master' into acton
daxfohl a9b7d61
Merge branch 'master' into acton
daxfohl 4d380d3
declare type of bits
daxfohl e9bf800
Merge remote-tracking branch 'origin/acton' into acton
daxfohl 4df3e7e
remove duplicate function
daxfohl 3479d21
Merge branch 'master' into acton
daxfohl 6eccee9
Cleanup
daxfohl 475aa12
Merge remote-tracking branch 'origin/acton' into acton
daxfohl 9fc6e08
Merge branch 'master' into acton
daxfohl 785c200
Merge branch 'master' into acton
daxfohl 6ed67c2
Remove `instanceof`s from `measurement_gate` by delegation.
daxfohl 097bb09
Merge branch 'refactormeasure' into acton
daxfohl eb55871
Linting fix
daxfohl d2dc152
Merge branch 'master' into acton
daxfohl f27bd65
Merge branch 'master' into acton
daxfohl afe3017
Merge branch 'master' into acton
daxfohl 3559b52
Merge branch 'master' into acton
daxfohl d0a5e3a
Merge branch 'master' into acton
daxfohl fb9d8c3
Merge branch 'master' into acton
daxfohl cf24502
Fix comments
daxfohl c940275
Change issue link
daxfohl c78d5be
Merge branch 'master' into acton
daxfohl 54fd0c1
lint
daxfohl 1bd4cd6
make perform_measurement private
daxfohl f738de0
Better error message for simulation failures.
daxfohl 16150a9
Better error message for simulation failures.
daxfohl 3f1b370
Add unit tests
daxfohl e90dabe
Merge branch 'master' into acton
daxfohl bdbd48c
Merge branch 'master' into acton
daxfohl 363e84a
Change return TypeError to raise.
daxfohl 0ae8346
remove two generated png files
daxfohl 93a76e1
re-add png files
daxfohl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,134 @@ | ||
# Copyright 2018 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. | ||
"""Objects and methods for acting efficiently on a density matrix.""" | ||
|
||
from typing import Any, Iterable, Dict, List, Sequence, Tuple | ||
|
||
import numpy as np | ||
|
||
from cirq import protocols, ops | ||
from cirq.protocols.decompose_protocol import _try_decompose_into_operations_and_qubits | ||
|
||
|
||
class ActOnDensityMatrixArgs: | ||
"""State and context for an operation acting on a density matrix. | ||
|
||
There are three common ways to act on this object: | ||
|
||
1. Directly edit the `target_tensor` property, which is storing the density | ||
matrix of the quantum system as a numpy array with one axis per qudit. | ||
2. Overwrite the `available_buffer` property with the new state vector, and | ||
then pass `available_buffer` into `swap_target_tensor_for`. | ||
3. Call `record_measurement_result(key, val)` to log a measurement result. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
target_tensor: np.ndarray, | ||
available_buffer: List[np.ndarray], | ||
axes: Iterable[int], | ||
qid_shape: Tuple[int, ...], | ||
prng: np.random.RandomState, | ||
log_of_measurement_results: Dict[str, Any], | ||
): | ||
""" | ||
Args: | ||
target_tensor: The state vector to act on, stored as a numpy array | ||
with one dimension for each qubit in the system. Operations are | ||
expected to perform inplace edits of this object. | ||
available_buffer: A workspace with the same shape and dtype as | ||
`target_tensor`. Used by operations that cannot be applied to | ||
`target_tensor` inline, in order to avoid unnecessary | ||
allocations. Passing `available_buffer` into | ||
`swap_target_tensor_for` will swap it for `target_tensor`. | ||
axes: The indices of axes corresponding to the qubits that the | ||
operation is supposed to act upon. | ||
qid_shape: The shape of the target tensor. | ||
prng: The pseudo random number generator to use for probabilistic | ||
effects. | ||
log_of_measurement_results: A mutable object that measurements are | ||
being recorded into. Edit it easily by calling | ||
`ActOnStateVectorArgs.record_measurement_result`. | ||
""" | ||
self.target_tensor = target_tensor | ||
self.available_buffer = available_buffer | ||
self.axes = tuple(axes) | ||
self.qid_shape = qid_shape | ||
self.prng = prng | ||
self.log_of_measurement_results = log_of_measurement_results | ||
|
||
def record_measurement_result(self, key: str, value: Any): | ||
"""Adds a measurement result to the log. | ||
|
||
Args: | ||
key: The key the measurement result should be logged under. Note | ||
that operations should only store results under keys they have | ||
declared in a `_measurement_keys_` method. | ||
value: The value to log for the measurement. | ||
""" | ||
if key in self.log_of_measurement_results: | ||
raise ValueError(f"Measurement already logged to key {key!r}") | ||
self.log_of_measurement_results[key] = value | ||
|
||
def _act_on_fallback_(self, action: Any, allow_decompose: bool): | ||
daxfohl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Apply channel to state.""" | ||
result = protocols.apply_channel( | ||
action, | ||
args=protocols.ApplyChannelArgs( | ||
target_tensor=self.target_tensor, | ||
out_buffer=self.available_buffer[0], | ||
auxiliary_buffer0=self.available_buffer[1], | ||
auxiliary_buffer1=self.available_buffer[2], | ||
left_axes=self.axes, | ||
right_axes=[e + len(self.qid_shape) for e in self.axes], | ||
), | ||
default=None, | ||
) | ||
if result is not None: | ||
for i in range(3): | ||
if result is self.available_buffer[i]: | ||
self.available_buffer[i] = self.target_tensor | ||
self.target_tensor = result | ||
return True | ||
|
||
if allow_decompose: | ||
return _strat_act_on_density_matrix_from_apply_decompose(action, self) | ||
|
||
return NotImplemented | ||
|
||
|
||
def _strat_act_on_density_matrix_from_apply_decompose( | ||
val: Any, | ||
args: ActOnDensityMatrixArgs, | ||
) -> bool: | ||
operations, qubits, _ = _try_decompose_into_operations_and_qubits(val) | ||
if operations is None: | ||
return NotImplemented | ||
return _act_all_on_density_matrix(operations, qubits, args) | ||
|
||
|
||
def _act_all_on_density_matrix( | ||
actions: Iterable[Any], qubits: Sequence[ops.Qid], args: ActOnDensityMatrixArgs | ||
): | ||
assert len(qubits) == len(args.axes) | ||
qubit_map = {q: args.axes[i] for i, q in enumerate(qubits)} | ||
|
||
old_axes = args.axes | ||
try: | ||
for action in actions: | ||
args.axes = tuple(qubit_map[q] for q in action.qubits) | ||
protocols.act_on(action, args) | ||
finally: | ||
args.axes = old_axes | ||
return True |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.