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

Fix bugs in to_chain_structure #168

Merged
merged 6 commits into from
Apr 15, 2023
Merged

Conversation

BoxiLi
Copy link
Member

@BoxiLi BoxiLi commented Sep 8, 2022

  • Copy the circuit correctly in to_chain_structure
  • Correctly handle measurement in the circuit.

Fix #165

- Copy the circuit correctly in to_chain_structure
- Correctly handle measurement in the circuit.
@BoxiLi BoxiLi marked this pull request as draft September 8, 2022 08:46
Copy link
Contributor

@hodgestar hodgestar left a comment

Choose a reason for hiding this comment

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

Properly copying the circuit looks like the right approach, but let's implement it properly on the circuit class itself.

@@ -27,7 +28,8 @@ def to_chain_structure(qc, setup="linear"):
# FIXME This huge block has been here for a long time.
# It could be moved to the new compiler section and carefully
# splitted into smaller peaces.
qc_t = QubitCircuit(qc.N, qc.reverse_states)
qc_t = deepcopy(qc)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does QubitCircuit implement its own copy support? E.g. an implementation of __copy__ or __deepcopy__. If not, I recommend adding it now and not relying on the default deepcopy which just copies all attributes and is not guaranteed to be sensible for this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the quick feedback! What would be the advantages of implementing our own copy? I feel like the implementation will just be copying a few attributes. And if a new attribute is added, the method also has to be updated every time.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a bit tough to explain succinctly, because in the very simple cases the default copy will work. The issue is that it is not guaranteed to work and there are many many possible edge cases where it won't work. This is why __copy__ and __deepcopy__ exist.

If you don't explicitly implement __copy__ then someone modifying QubitCircuit might do something that causes __copy__ to do the wrong thing, and then code that calls copy would be safe.

If we implement __copy__ then we are committing to supporting copy(qc) working. I.e. we're making it part of our API, and that seems like a good thing. Without __copy__ we are making no such guarantee.

QubitCircuit maintains quite a lot of internal state and just copying that state might do the wrong thing. I wouldn't be able to tell for sure that the default copy does the right thing without checking thoroughly, and that is part of why we should write our own __copy__ -- because it would be guaranteed to do the right thing for QubitCircuit while the default would not (boh now and in the future).

I'm about 90% sure I could dig into QubitCircuit and find a problem, but whether there actually is an issue is not the point. The point is to provide certainty that copy will work and to make it clear to ourselves that we are committing to make it work.

It's entirely possible that the implementation of __copy__ might be somewhat trivial, and that's completely okay.

@BoxiLi
Copy link
Member Author

BoxiLi commented Sep 9, 2022

Not really very confident about my implementation. Let's discuss next week @hodgestar :)

@hodgestar
Copy link
Contributor

@BoxiLi Let's review and rework a bit if needed on Monday.

@BoxiLi BoxiLi added this to the qutip-qip-0.2.3 milestone Sep 11, 2022
@BoxiLi
Copy link
Member Author

BoxiLi commented Mar 14, 2023

Hi Simon, would it be ok if we just fix the error by using deepcopy here and move a full functional implementation of Circuit.copy() to another PR? Of course a test is till missing here.

@hodgestar
Copy link
Contributor

I'm also okay with a simpler fix if you prefer.

@BoxiLi BoxiLi marked this pull request as ready for review April 10, 2023 14:00
@BoxiLi
Copy link
Member Author

BoxiLi commented Apr 10, 2023

Fix the bug in to_chain_structure only. Leave the circuit copying feature for a different PR.

The failing test is because of qtuip-qtrl. We should use the master branch instead of the released ones for the check of qutip.master branch.

@hodgestar
Copy link
Contributor

I see there is also a warning for DeprecationWarning: invalid escape sequence '\s' in the tests that failed, but that can be left for another PR.

@BoxiLi
Copy link
Member Author

BoxiLi commented Apr 11, 2023

I see there is also a warning for DeprecationWarning: invalid escape sequence '\s' in the tests that failed, but that can be left for another PR.

Yes... That is likely the docsting using LaTeX without adding an r.

@BoxiLi BoxiLi merged commit 72aad5d into qutip:master Apr 15, 2023
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.

Chain transpiler calls QubitCircuit with incorrect arguments
2 participants