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

Measurement in Pauli basis #799

Merged
merged 12 commits into from
Mar 9, 2023
Merged

Measurement in Pauli basis #799

merged 12 commits into from
Mar 9, 2023

Conversation

stavros11
Copy link
Member

@stavros11 stavros11 commented Feb 17, 2023

Fixes #720. I am copying here the TODO list from #720 (comment)

  • add a basis argument for gates.M which accepts gate objects (instead of strings).
  • modify circuit execution to accept initial states of two types: arrays and circuits (to be able to execute on hardware).
  • provide a set of function initializers for most common states, e.g. |+>, |->, |1>, |+i>, |-i> among others, using circuits.

I implemented the first point. @andrea-pasquale @Edoardo-Pedicillo let me know if you would like to have a look at any of the others.

Note that as discussed using the basis argument in the measurement gate will add additional gates that rotate to this basis before the measurement. For example:

c = Circuit(1)
c.add(gates.M(0, basis=gates.X)

will result to

0: --H--M

Also, I implemented the basis rotation only for the Pauli gates (X, Y, Z) as this was requested in the original issue, however it may be possible to do it for any gate. Essentially for a single qubit gate G we need to solve U^dagger Z U = G for U.

Checklist:

  • Reviewers confirm new code works as expected.
  • Tests are passing.
  • Coverage does not decrease.
  • Documentation is updated.

@codecov
Copy link

codecov bot commented Feb 17, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change

Comparison is base (a1726b8) 100.00% compared to head (379886f) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #799   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           47        47           
  Lines         5726      5752   +26     
=========================================
+ Hits          5726      5752   +26     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/qibo/backends/numpy.py 100.00% <100.00%> (ø)
src/qibo/gates/abstract.py 100.00% <100.00%> (ø)
src/qibo/gates/gates.py 100.00% <100.00%> (ø)
src/qibo/gates/measurements.py 100.00% <100.00%> (ø)
src/qibo/models/circuit.py 100.00% <100.00%> (ø)

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

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@renatomello
Copy link
Contributor

renatomello commented Mar 6, 2023

Does this also work for when a multi-qubit measurement M object is created?

Also, it would be good to pass just a string instead of the gate object, as in

c = Circuit(2)
c.add(gates.M(0, 1, basis="XY")

@stavros11
Copy link
Member Author

After discussing with @renatomello, extending this to support a different basis for each qubit measured could be useful. In summary this could accept:

  • a single gate class to be applied on all qubits (already implemented),
  • a list of gate objects that has the same length as the measured qubits and specifies the basis for each qubit (TODO).

The string representation proposed in the comment above could also be good as it is more compact, however I am a bit against supporting many different types as inputs to a function as it is harder to implement, maintain and document and as a result could also be more confusing for users. With the above addition we already accept two types (gate class or list) and if we add strings we go up to four which could be complicated to handle.

@scarrazza let me know what you think.

@scarrazza
Copy link
Member

I would suggest to keep the gate object and list, but avoid the string because we don't use this interfacing mechanism (string-gate) anywhere in qibo, and it is easier to debug for the user.

@scarrazza
Copy link
Member

Following the discussion today, we agreed to add tests and merge.

@stavros11
Copy link
Member Author

stavros11 commented Mar 8, 2023

Following the discussion today, we agreed to add tests and merge.

I fixed the conflicts and I also implemented the list for basis as I realized it was a quick update.

@renatomello if you like give a try and let me know if it is what you had in mind. Your example should be modified to

c = Circuit(2)
c.add(gates.M(0, 1, basis=[gates.X, gates.Y])

since we decided to not accept strings.

@stavros11 stavros11 requested a review from renatomello March 8, 2023 12:05
@renatomello
Copy link
Contributor

renatomello commented Mar 8, 2023

Following the discussion today, we agreed to add tests and merge.

I fixed the conflicts and I also implemented the list for basis as I realized it was a quick update.

@renatomello if you like give a try and let me know if it is what you had in mind. Your example should be modified to

c = Circuit(2)
c.add(gates.M(0, 1, basis=[gates.X, gates.Y])

since we decided to not accept strings.

I tried this code

from qibo import gates
from qibo.models import Circuit

nqubits = 2
circuit = Circuit(nqubits)

basis = "XY"
basis = [getattr(gates, pauli) for pauli in basis]
circuit.add(gates.M(*range(nqubits), basis=basis))

print(circuit.draw())

and the gates are being added after the measurement.

@stavros11
Copy link
Member Author

Thanks for checking, should be fixed now.

@renatomello
Copy link
Contributor

It works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Mesurement and initialization in Pauli basis
5 participants