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

Reduce redundancy in act_on_fallback #4400

Closed
daxfohl opened this issue Aug 10, 2021 · 1 comment · Fixed by #5111
Closed

Reduce redundancy in act_on_fallback #4400

daxfohl opened this issue Aug 10, 2021 · 1 comment · Fixed by #5111
Labels
area/simulation kind/design-issue A conversation around design

Comments

@daxfohl
Copy link
Contributor

daxfohl commented Aug 10, 2021

Describe your design idea/issue

Ref #4371 (comment)

act_on_fallback accepts both operations and gate+qubit[] pairs. This generally causes some redundancy and occasional sloppiness in the implementations. In MPS simulator, we just promote gate+qubit[] to a GateOperation and work with that so that everything is then consistent.

This may be the best approach for all simulators, and we could do this in the act_on protocol itself, so that act_on_fallback only needs to accept operations. The main concern with that approach would be if it gets into an infinite loop of act_on(gate, qubits) -> act_on_fallback(gate_op) -> act_on(gate, qubits) -> .... However I don't think any act_on_fallbacks do this, nor should they, as gate.act_on gets called via gate_op.act_on in the initial pass, so there's nothing to be gained by doing this.

@daxfohl
Copy link
Contributor Author

daxfohl commented Jan 12, 2022

Once #4705 and #4702 are merged, we can actually go in the opposite direction. We require all Operations either expose a gate, or use _act_on_ to implement their own simulation logic, and pass the gate into act_on_fallback. This feels clean to me: we should have two types of operations:

  • Operations that have a gate that performs its logic (if it's a quantum op)
  • Operations that implement the logic in _act_on_ (if it's a control op, e.g. things that don't need to know which simulator they're using and execute the same logic regardless, like CircuitOperation or ClassicallyControlledOperation)

Then in protocols.act_on before calling the _act_on_fallback_ method, we get the gate from the action (which it MUST have (or be), to get to that point in the code), and pass that in as the first parameter to fallback. (We'd have to emit a deprecation warning here initially for external ops that don't have gates or act_on implementations).

But that allows us to clean up the code (no more passing qubits redundantly with operations), solidify the design intent, and have stronger guarantees on what gets passed into act_on_fallback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/simulation kind/design-issue A conversation around design
Projects
None yet
2 participants