Skip to content

Add method to get "mapped_circuit" from CircuitOperations #4188

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 9 commits into from
Jun 24, 2021

Conversation

95-martin-orion
Copy link
Collaborator

This PR replaces #3785, bringing it up to date with the current state of MeasurementKey behavior and adding tests. Use of the mapped_circuit method will allow the moment structure of CircuitOperations to be preserved during deserialization.

maffoo and others added 6 commits February 10, 2021 16:24
The mapped circuit is equivalent to the inner circuit, but with various
maps and transformations applied (qubit map, param map, measurement map,
repetitions, repetition ids). The mapping can be applied deeply, in
which case contained CircuitOperations are expanded recursively, or
shallowly, in which case contained CircuitOperations are left unchanged.

To implement the deep mapping, we add a map_ops method on
AbstractCircuit that applies a function to each op in the circuit and
then recombines them in a way that preserves moment structure as much as
possible.

Fixes #3783
@95-martin-orion 95-martin-orion requested review from cduck, vtomole and a team as code owners June 11, 2021 14:51
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Jun 11, 2021
@95-martin-orion 95-martin-orion requested review from tanujkhattar and removed request for smitsanghavi June 17, 2021 14:50
Copy link
Contributor

@maffoo maffoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! Would be nice to add a test case with measurements in a repeated op to see that key paths are remapped properly.

circuit.map_ops(lambda op: func(op, rep)) for rep in self.repetition_ids
)

def mapped_op(self, deep: bool = False) -> 'cirq.CircuitOperation':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstring

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, although I have question on this. I would normally omit docstrings for one-line methods - is the devsite documentation the primary motivation for including a docstring in such cases?

Copy link
Contributor

@maffoo maffoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

result = self.circuit.unfreeze()
result = result.transform_qubits(lambda q: self.qubit_map.get(q, q))
def mapped_circuit(self, deep: bool = False) -> 'cirq.Circuit':
"""Applies all maps to the cotained circuit and returns the result.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Applies all maps to the cotained circuit and returns the result.
"""Applies all maps to the contained circuit and returns the result.

Comment on lines 192 to 193
deep: If true, this will also call mapped_circuit on any
CircuitOperations this object contains.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
deep: If true, this will also call mapped_circuit on any
CircuitOperations this object contains.
deep: If true, this will also call mapped_circuit on any
CircuitOperations this object contains.

@95-martin-orion 95-martin-orion merged commit 36e5f60 into master Jun 24, 2021
@95-martin-orion 95-martin-orion deleted the cirq-mapped-circuit-0 branch June 24, 2021 19:25
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…#4188)

* Add method to get "mapped_circuit" from CircuitOperation

The mapped circuit is equivalent to the inner circuit, but with various
maps and transformations applied (qubit map, param map, measurement map,
repetitions, repetition ids). The mapping can be applied deeply, in
which case contained CircuitOperations are expanded recursively, or
shallowly, in which case contained CircuitOperations are left unchanged.

To implement the deep mapping, we add a map_ops method on
AbstractCircuit that applies a function to each op in the circuit and
then recombines them in a way that preserves moment structure as much as
possible.

Fixes quantumlib#3783

* Rework to handle repetition_ids properly when deep=False

* Modernize and test mapped_circuit

* Formatting

* Clearer function names and docstrings.

* Fix typos

Co-authored-by: Matthew Neeley <[email protected]>
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…#4188)

* Add method to get "mapped_circuit" from CircuitOperation

The mapped circuit is equivalent to the inner circuit, but with various
maps and transformations applied (qubit map, param map, measurement map,
repetitions, repetition ids). The mapping can be applied deeply, in
which case contained CircuitOperations are expanded recursively, or
shallowly, in which case contained CircuitOperations are left unchanged.

To implement the deep mapping, we add a map_ops method on
AbstractCircuit that applies a function to each op in the circuit and
then recombines them in a way that preserves moment structure as much as
possible.

Fixes quantumlib#3783

* Rework to handle repetition_ids properly when deep=False

* Modernize and test mapped_circuit

* Formatting

* Clearer function names and docstrings.

* Fix typos

Co-authored-by: Matthew Neeley <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants