Skip to content
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

Split the simulators' creation of ActOnArgs and iteration #3970

Merged
merged 52 commits into from
Apr 20, 2021

Conversation

daxfohl
Copy link
Contributor

@daxfohl daxfohl commented Mar 25, 2021

Fixes #3872.

Currently all simulators implement _base_iterator, which does two things: it creates the ActOnArgs object, and the iterates the circuit. This PR splits ActOnArgs creation and iteration into two separate functions in each of the simulators. This allows preservation of single-responsibility principle in the simulator methods. It potentially allows moving _core_iterator into the base class to remove that redundancy in a future PR (all four simulators' implementations of _core_iterator are almost identical now).

It also addresses the problem in #3958 with a workaround: the user can now propagate the ActOnArgs to subsequent circuits. See the quantum teleportation example for an example of how to do this. Note that this process works for all simulators, and a function can be written generically to support any of them, so long as the same simulator runs each circuit.

The quantum teleportation is refactored to be implemented in terms of this. This fixes the "hack" of the teleportation in a different way than the classical control tags. Here, the circuit is split into two, and the Bob side is constructed and run after the Alice side based on Alice's measurements. Unlike the tags formulation, this cannot be serialized into a single circuit and requires the Python code to manage the classical coordination. Again, other than the final state vector printout, this example can work generically with any simulator.

Open question: should these functions be public?

@daxfohl daxfohl requested review from cduck, vtomole and a team as code owners March 25, 2021 17:26
@google-cla
Copy link

google-cla bot commented Mar 25, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Mar 25, 2021
@daxfohl
Copy link
Contributor Author

daxfohl commented Mar 25, 2021

@tonybruguier can you consent here too? Seems like I must have merged wonkily on this one.

@tonybruguier
Copy link
Contributor

@googlebot I consent.

@google-cla google-cla bot added cla: yes Makes googlebot stop complaining. and removed cla: no labels Mar 25, 2021
@daxfohl daxfohl requested a review from 95-martin-orion April 4, 2021 00:30
cirq/sim/simulator.py Outdated Show resolved Hide resolved
cirq/sim/simulator.py Outdated Show resolved Hide resolved
cirq/sim/simulator.py Outdated Show resolved Hide resolved
@daxfohl
Copy link
Contributor Author

daxfohl commented Apr 19, 2021

@95-martin-orion I think this would be easier to pull the trigger on if I made create_act_on_args a private function for now and reverted the teleportation example. That would give us time to ponder whether "ActOnArgs" is the right name for this etc, before going all public with it. That would make this purely a refactoring change. Thoughts?

@95-martin-orion
Copy link
Collaborator

Apologies for the wait! I'll be prioritizing review of this PR this week.

@95-martin-orion I think this would be easier to pull the trigger on if I made create_act_on_args a private function {...}

Let's hold off on this change for now. I'll review the latest changes and see how much of this PR (if anything )should be saved for subsequent PRs.

Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

Having reviewed the new changes, I'm inclined to agree with making _create_act_on_args private to avoid "going public" with this behavior for now. Other than that, I'm largely happy with the structure of this PR.

cirq/sim/clifford/clifford_simulator.py Outdated Show resolved Hide resolved
cirq/google/calibration/engine_simulator.py Outdated Show resolved Hide resolved
examples/quantum_teleportation.py Outdated Show resolved Hide resolved
cirq/sim/simulator.py Outdated Show resolved Hide resolved
daxfohl added 2 commits April 19, 2021 12:45
* create_act_on_args private
* Allow CliffordState to take ch_form as an initial state
* type annotations
* revert quantum teleportation example
@daxfohl
Copy link
Contributor Author

daxfohl commented Apr 19, 2021

@95-martin-orion The requested changes have been made.

@95-martin-orion
Copy link
Collaborator

LGTM. Since the big cirq_google PR (#3957) is expected to land today, let's hold this until after it goes through. The fix-up process should be lightweight since this PR only touches one cirq.google file.

Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

Balint sent out an email to the cirq-dev group with details on how to sync past the cirq_google change. It shouldn't be too complicated for this PR - let me know if you hit any snags.

@daxfohl
Copy link
Contributor Author

daxfohl commented Apr 20, 2021

@95-martin-orion Done

@95-martin-orion 95-martin-orion merged commit 1514cec into quantumlib:master Apr 20, 2021
@daxfohl daxfohl deleted the spliterate branch April 20, 2021 16:45
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…#3970)

* Move MPSSimulator to use act_on

* Add axes to mps

* Finish act-on for mps

* lint

* Add a copy method for all act_on_args

* Split creation of act_on_args and iteration

* remove the empty yields

* Fix most unit tests

* Change sparse simulator to use act_on_args in _run.

* Fix clifford simulator

* format

* optimize imports

* Address PR comments

* Unit test

* Formatting

* Rename, format

* Fix docs

* Update quantum teleportation

* Update quantum teleportation

* Update quantum teleportation

* Make the QubitOrder arg optional

* Fix the engine_simulator.py overrides

* Fix the engine_simulator.py overrides

* Fix the engine_simulator.py overrides

* Fix coverage check

* Change quantum teleportation example to be generic across simulators.

* Change quantum teleportation example to be generic across simulators.

* Change qubit_order to an explicit ordering of qubits.

* Clean PR issues

* Fix type check

* Fix unit test

* Spelling

* small changes

* test change

* make _core_iterator.sim_state naming consistent

* Allow create_act_on_args to accept an ActOnArgs parameter, and return it directly if supplied.

* Add tests for `.simulate(ActOnArgs)`.

* Add tests for `.simulate(ActOnArgs)`.

* lint

* Plop qubits and qubit_map into ActOnArgs instead of passing together everywhere.

* Change qubits param to Sequence. Clean up teleportation example.

* PR comments:

* create_act_on_args private
* Allow CliffordState to take ch_form as an initial state
* type annotations
* revert quantum teleportation example

* format/lint

* format

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

* Move MPSSimulator to use act_on

* Add axes to mps

* Finish act-on for mps

* lint

* Add a copy method for all act_on_args

* Split creation of act_on_args and iteration

* remove the empty yields

* Fix most unit tests

* Change sparse simulator to use act_on_args in _run.

* Fix clifford simulator

* format

* optimize imports

* Address PR comments

* Unit test

* Formatting

* Rename, format

* Fix docs

* Update quantum teleportation

* Update quantum teleportation

* Update quantum teleportation

* Make the QubitOrder arg optional

* Fix the engine_simulator.py overrides

* Fix the engine_simulator.py overrides

* Fix the engine_simulator.py overrides

* Fix coverage check

* Change quantum teleportation example to be generic across simulators.

* Change quantum teleportation example to be generic across simulators.

* Change qubit_order to an explicit ordering of qubits.

* Clean PR issues

* Fix type check

* Fix unit test

* Spelling

* small changes

* test change

* make _core_iterator.sim_state naming consistent

* Allow create_act_on_args to accept an ActOnArgs parameter, and return it directly if supplied.

* Add tests for `.simulate(ActOnArgs)`.

* Add tests for `.simulate(ActOnArgs)`.

* lint

* Plop qubits and qubit_map into ActOnArgs instead of passing together everywhere.

* Change qubits param to Sequence. Clean up teleportation example.

* PR comments:

* create_act_on_args private
* Allow CliffordState to take ch_form as an initial state
* type annotations
* revert quantum teleportation example

* format/lint

* format

Co-authored-by: Tony Bruguier <[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.

[RFC] Split _base_iterator into two functions
3 participants