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

OptimizeSwapBeforeMeasure only when last measurements #5906

Closed
wants to merge 6 commits into from

Conversation

1ucian0
Copy link
Member

@1ucian0 1ucian0 commented Feb 25, 2021

Fixes #5905

jlapeyre
jlapeyre previously approved these changes Feb 25, 2021
Copy link
Contributor

@jlapeyre jlapeyre left a comment

Choose a reason for hiding this comment

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

I can't find any problem with this. Pulled the branch and played with it, verified that it solves the issue, and tests pass. But, someone more familiar with the transpiler should review, as well.

You might consider adding a comment or two. Especially since the method was completely rewritten and a couple of comments were lost.

Copy link
Member

@kdk kdk left a comment

Choose a reason for hiding this comment

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

I agree with @jlapeyre that added comments would be helpful here. Also, the pass now rebuilds the entire circuit, rather than scanning and substituting only around swaps and measures, which would be more costly for large circuits. Can this be avoided?

Comment on lines +39 to +45
new_dag = DAGCircuit()
new_dag.metadata = dag.metadata
new_dag._global_phase = dag._global_phase
for creg in dag.cregs.values():
new_dag.add_creg(creg)
for qreg in dag.qregs.values():
new_dag.add_qreg(qreg)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
new_dag = DAGCircuit()
new_dag.metadata = dag.metadata
new_dag._global_phase = dag._global_phase
for creg in dag.cregs.values():
new_dag.add_creg(creg)
for qreg in dag.qregs.values():
new_dag.add_qreg(qreg)
new_dag = dag._copy_circuit_metadata()

Copy link
Member Author

Choose a reason for hiding this comment

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

nice one. Why is _copy_circuit_metadata a private method?

Copy link
Member

Choose a reason for hiding this comment

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

Initially because it was used internally, but then was useful to passes that needed to rebuild the dag (but preserve metadata) like the layout routing passes. Making it public seems like a good idea.

for node in dag.topological_op_nodes():
if node.type == 'op':
qargs = [_trivial_layout[_layout[qarg]] for qarg in node.qargs]
if isinstance(node.op, SwapGate):
Copy link
Member

Choose a reason for hiding this comment

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

Does this apply for all swaps or only those added by routing?

Copy link
Member Author

Choose a reason for hiding this comment

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

does not distinguish from where is the SWAP coming from.

@1ucian0
Copy link
Member Author

1ucian0 commented Mar 1, 2021

Also, the pass now rebuilds the entire circuit, rather than scanning and substituting only around swaps and measures, which would be more costly for large circuits. Can this be avoided?

The logic of a local replacement was getting hard with more "looking ahead" conditions. Now conditions are more complex like a swap followed by a measurement (in both wires), followed by an out node.

Things get even harder with #5890 on top of this one. Because I'm trying to partially preserve previous behaviour as an option in #5890, it will be possible to remove a swap mid circuit. This breaks the locality of the transformation (ie, following gates need to be moved to a different wire).

I could try to keep the structure of the pass as it was, but that will result in an incompatible pass wrt to the current OptimizeSwapBeforeMeasure or in very complicated code. In this case, probably create a new pass instead of modifying this one. What do you think?

@1ucian0 1ucian0 marked this pull request as draft May 17, 2021 12:11
@jakelishman
Copy link
Member

This appears to have been obsoleted by #11413, which fixed the final-measurement checker a bit more completely. I'll close this PR as obsolete now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OptimizeSwapBeforeMeasure should only affect last measurements, not mid-circuit measurements
4 participants