-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Perform CliffordState measurements via method dispatch #3503
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 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
10ce909
Perform CliffordState measurements via method dispatch
smitsanghavi 6f5f227
Merge remote-tracking branch 'upstream/master' into clifmeas
smitsanghavi 27a6541
Merge branch 'master' into clifmeas
smitsanghavi 6e203cf
format
smitsanghavi b88c87c
Merge branch 'master' into clifmeas
smitsanghavi 9301bf1
Deprecate uses of CliffordTableau
smitsanghavi 64d127b
Increase the deprecation deadline version
smitsanghavi 52c91d3
Validate that apply_measurement gets a measurement op
smitsanghavi b20195a
Merge branch 'master' into clifmeas
CirqBot 03aab29
Merge branch 'master' into clifmeas
CirqBot 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,8 +106,7 @@ def _base_iterator( | |
|
||
for op in moment: | ||
if isinstance(op.gate, ops.MeasurementGate): | ||
key = protocols.measurement_key(op) | ||
measurements[key].extend(state.perform_measurement(op.qubits, self._prng)) | ||
state.apply_measurement(op, measurements, self._prng) | ||
elif protocols.has_unitary(op): | ||
state.apply_unitary(op) | ||
else: | ||
|
@@ -245,16 +244,17 @@ def sample( | |
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None, | ||
) -> np.ndarray: | ||
|
||
measurements = [] | ||
measurements = {} # type: Dict[str, List[np.ndarray]] | ||
|
||
for _ in range(repetitions): | ||
measurements.append( | ||
self.state.perform_measurement( | ||
qubits, value.parse_random_state(seed), collapse_state_vector=False | ||
) | ||
for i in range(repetitions): | ||
self.state.apply_measurement( | ||
cirq.measure(*qubits, key=str(i)), | ||
measurements, | ||
value.parse_random_state(seed), | ||
collapse_state_vector=False, | ||
) | ||
|
||
return np.array(measurements, dtype=bool) | ||
return np.array(list(measurements.values()), dtype=bool) | ||
|
||
|
||
@value.value_equality | ||
|
@@ -312,11 +312,13 @@ def __str__(self) -> str: | |
def to_numpy(self) -> np.ndarray: | ||
return self.ch_form.to_state_vector() | ||
|
||
@deprecated(deadline='v0.11.0', fix='use CliffordTableau instead') | ||
def stabilizers(self) -> List[DensePauliString]: | ||
"""Returns the stabilizer generators of the state. These | ||
are n operators {S_1,S_2,...,S_n} such that S_i |psi> = |psi>""" | ||
return self.tableau.stabilizers() | ||
|
||
@deprecated(deadline='v0.11.0', fix='use CliffordTableau instead') | ||
def destabilizers(self) -> List[DensePauliString]: | ||
"""Returns the destabilizer generators of the state. These | ||
are n operators {S_1,S_2,...,S_n} such that along with the stabilizer | ||
|
@@ -331,21 +333,40 @@ def wave_function(self): | |
return self.state_vector() | ||
|
||
def apply_unitary(self, op: 'cirq.Operation'): | ||
tableau_args = clifford.ActOnCliffordTableauArgs( | ||
self.tableau, [self.qubit_map[i] for i in op.qubits], np.random.RandomState(), {} | ||
) | ||
ch_form_args = clifford.ActOnStabilizerCHFormArgs( | ||
self.ch_form, [self.qubit_map[i] for i in op.qubits], np.random.RandomState(), {} | ||
) | ||
try: | ||
act_on(op, tableau_args) | ||
act_on(op, ch_form_args) | ||
except TypeError: | ||
raise ValueError( | ||
'%s cannot be run with Clifford simulator.' % str(op.gate) | ||
) # type: ignore | ||
return | ||
|
||
def apply_measurement( | ||
self, | ||
op: 'cirq.Operation', | ||
measurements: Dict[str, List[np.ndarray]], | ||
prng: np.random.RandomState, | ||
collapse_state_vector=True, | ||
): | ||
if not isinstance(op.gate, cirq.MeasurementGate): | ||
raise TypeError( | ||
'apply_measurement only supports cirq.MeasurementGate operations. Found %s instead.' | ||
% str(op.gate) | ||
) | ||
|
||
if collapse_state_vector: | ||
state = self | ||
else: | ||
state = self.copy() | ||
|
||
qids = [self.qubit_map[i] for i in op.qubits] | ||
|
||
ch_form_args = clifford.ActOnStabilizerCHFormArgs(state.ch_form, qids, prng, measurements) | ||
act_on(op, ch_form_args) | ||
|
||
@deprecated_parameter( | ||
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. Given that the whole function is deprecated now, should I remove the param deprecation annotation? |
||
deadline='v0.10.0', | ||
fix='Use collapse_state_vector instead.', | ||
|
@@ -359,6 +380,7 @@ def apply_unitary(self, op: 'cirq.Operation'): | |
}, | ||
), | ||
) | ||
@deprecated(deadline='v0.11.0', fix='Use the apply_measurement instead') | ||
def perform_measurement( | ||
self, qubits: Sequence[ops.Qid], prng: np.random.RandomState, collapse_state_vector=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
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.