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

Refactoring of NoisyOperation, NoisyBasis, OperationRepresentation #1712

Merged
merged 19 commits into from Feb 28, 2023

Conversation

andreamari
Copy link
Member

@andreamari andreamari commented Feb 10, 2023

Fixes: #1659

License

  • [ x] I license this contribution under the terms of the GNU GPL, version 3 and grant Unitary Fund the right to provide additional permissions as described in section 7 of the GNU GPL, version 3.

@github-actions
Copy link

Binder 👈 Launch a binder notebook on branch unitaryfund/mitiq/improve-pec-types

@codecov
Copy link

codecov bot commented Feb 10, 2023

Codecov Report

Merging #1712 (0dbc4b2) into master (fe04e5e) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1712   +/-   ##
=======================================
  Coverage   98.49%   98.49%           
=======================================
  Files          68       68           
  Lines        3186     3187    +1     
=======================================
+ Hits         3138     3139    +1     
  Misses         48       48           
Impacted Files Coverage Δ
mitiq/pec/types/__init__.py 100.00% <ø> (ø)
mitiq/pec/__init__.py 100.00% <100.00%> (ø)
mitiq/pec/representations/biased_noise.py 100.00% <100.00%> (ø)
mitiq/pec/representations/damping.py 100.00% <100.00%> (ø)
mitiq/pec/representations/depolarizing.py 100.00% <100.00%> (ø)
mitiq/pec/representations/optimal.py 97.14% <100.00%> (-0.16%) ⬇️
mitiq/pec/sampling.py 100.00% <100.00%> (ø)
mitiq/pec/types/types.py 100.00% <100.00%> (ø)
mitiq/calibration/settings.py 100.00% <0.00%> (ø)
mitiq/calibration/calibrator.py 98.87% <0.00%> (+0.26%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@andreamari andreamari mentioned this pull request Feb 14, 2023
1 task
@andreamari andreamari marked this pull request as ready for review February 21, 2023 16:49
@andreamari
Copy link
Member Author

@Misty-W and/or @natestemen , I am fighting with some silly style issues but all other tests are passing. So feel free to review this PR when you have some time.

Copy link
Member

@natestemen natestemen left a comment

Choose a reason for hiding this comment

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

Looks pretty good to me, but I don't think I fully understand why switching from { op: coeff } to [ ops ] and [ coeffs ] is better. I know in the ticket you mentioned having an ordering is nice, but seeing as there is a tight connection between the operations and coefficients, it does seem natural to me to have them stored in a dictionary. Can you just provide a little more detail on what this achieves?

In terms of future plans, we maybe remove NoisyBasis altogether in the release after the current one?

Comment on lines -72 to +71
ideal, _ = convert_to_mitiq(ideal_operation)
ideal, native_type = convert_to_mitiq(ideal_operation)
Copy link
Member

Choose a reason for hiding this comment

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

So this function could take any QPROGRAM, but previously always returned cirq.Circuits. Is my understanding right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure what type it was previously returning (since NoisyOperation.circuit was different) but, yes, it should return a QPROGRAM with the same input type.

Note also that an important update introduced here is that the qubits of the NoisyOperations are replaced by the qubits of the ideal operation that we aim to represent (the input argument of the function). This mapping is superfluous if NoisyOperation._is_qubit_dependent is True, but it is necessary when it is False.

I just added an inline suggestion to avoid useless conversions when NoisyOperation._is_qubit_dependent == True.

mitiq/pec/sampling.py Outdated Show resolved Hide resolved
Comment on lines 16 to 20
from mitiq.pec.types.types import (
NoisyBasis,
NoisyOperation,
OperationRepresentation,
NoisyBasis,
)
Copy link
Member

Choose a reason for hiding this comment

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

Does changing the order of imports affect anything, or just reordering to show level of importance?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is because I first removed NoisyBasis from Mitiq and then re-added a deprecated version of the NoisyBasis later. Reordering was not intentional.

Copy link
Contributor

@Misty-W Misty-W Feb 24, 2023

Choose a reason for hiding this comment

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

To confirm, is the idea of re-adding just to warn the user that NoisyBasis is deprecated?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. We can completely remove it in 1 or 2 milestones.

Technically, this is not a deprecation intended in the standard way, which would imply warning but still supporting NoisyBasis. In our case we just drop it and we give some info to the user with an error message.

andreamari and others added 2 commits February 24, 2023 09:50
Co-authored-by: nate stemen <nate@unitary.fund>
@andreamari
Copy link
Member Author

Looks pretty good to me, but I don't think I fully understand why switching from { op: coeff } to [ ops ] and [ coeffs ] is better. I know in the ticket you mentioned having an ordering is nice, but seeing as there is a tight connection between the operations and coefficients, it does seem natural to me to have them stored in a dictionary. Can you just provide a little more detail on what this achieves?

Some motivations:

  • [Main motivation] Using NoisyOperations as a keys of a dictionary, requires having a reliable and well-tested __hash__() function for the NoisyOperation class. We don't have one, so we have been using the default hash based on the memory id which is quite unsafe.
  • From a practical usage point of view, it's probably a matter of subjective preference. I like separated lists for representing objects in a vector space since one typically fixes the basis with a fixed ordering and, once this is done, one can represent objects via simple real vectors which can be easily transformed, optimized, sampled, etc... .
  • An example use case is when solving a numerical optimization problem to find the optimal coefficients. The optimal solution is a real vector of coefficients, but to what NoisyOperations do the coefficients correspond? This ambiguity is absent with the new approach.
  • Note that NoisyOperation.basis_expansion still exists and can be used to couple coefficients and basis elements.

mitiq/pec/sampling.py Outdated Show resolved Hide resolved
@Misty-W Misty-W changed the title Refactoring of NosiyOperation, NoisyBasis, OperationRepresentation Refactoring of NoisyOperation, NoisyBasis, OperationRepresentation Feb 24, 2023
Copy link
Member

@natestemen natestemen left a comment

Choose a reason for hiding this comment

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

:shipit:

@natestemen natestemen added this pull request to the merge queue Feb 28, 2023
Merged via the queue into master with commit ca1620d Feb 28, 2023
@Misty-W Misty-W deleted the improve-pec-types branch February 28, 2023 21:46
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.

Improve OperationRepresentation and NoisyOperation objects
3 participants