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 fixes and improvements #647

Merged
merged 36 commits into from
Dec 7, 2022
Merged

Measurement fixes and improvements #647

merged 36 commits into from
Dec 7, 2022

Conversation

stavros11
Copy link
Member

This PR implements various improvements in measurements, particularly:

  • Eliminates the circuit.measurement_gate and adds all measurements (with and without collapse) in the gate queue.
  • Makes possible to reuse (add more gates to) qubits that are measured. If the collapse=True option was not used when measuring a qubit that is reused then it is enabled automatically, as I am not aware of any other physical way that a qubit can be reused without collapsing it.
  • Fixes an issue with result = c.add(gates.M(0, 1, collapse=True)) which was out of sync with previous versions and the docs (thanks @igres26).
    Now the result is a MeasurementResult object which has result.samples(), result.frequencies() and also result.symbols which is a list of sympy.Symbols that can be used to condition later gates on the measurement outcomes.
  • The result object returned by c.add is now available for non-collapse measurements too.
    For example
from qibo import gates
from qibo.models import Circuit

c = Circuit(2)
c.add(gates.H(0))
c.add(gates.H(1))
m0 = c.add(gates.M(0))
m1 = c.add(gates.M(1))
r = c()

then

  • r is a CircuitResult object that contains the final state vector and all measurements.
  • m0 is a MeasurementResult object that contains the measurements of qubit 0.
  • m1 is a MeasurementResult object that contains the measurements of qubit 1.

This is complete in terms of developments but I am labeling it as "do not merge" as I would like to make sure it plays well with qibolab before merging.

@stavros11 stavros11 added the do not merge Blocked by some other development or PR label Sep 21, 2022
@codecov
Copy link

codecov bot commented Sep 21, 2022

Codecov Report

Base: 100.00% // Head: 100.00% // No change to project coverage 👍

Coverage data is based on head (3a92d4f) compared to base (513d302).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #647   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           88        88           
  Lines        11663     11702   +39     
=========================================
+ Hits         11663     11702   +39     
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/noise.py 100.00% <ø> (ø)
src/qibo/tests/test_gates_abstract.py 100.00% <ø> (ø)
src/qibo/backends/numpy.py 100.00% <100.00%> (ø)
src/qibo/gates/measurements.py 100.00% <100.00%> (ø)
src/qibo/gates/special.py 100.00% <100.00%> (ø)
src/qibo/models/circuit.py 100.00% <100.00%> (ø)
src/qibo/models/distcircuit.py 100.00% <100.00%> (ø)
src/qibo/states.py 100.00% <100.00%> (ø)
src/qibo/tests/test_dill.py 100.00% <100.00%> (ø)
src/qibo/tests/test_measurements.py 100.00% <100.00%> (ø)
... and 7 more

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.

Copy link
Contributor

@andrea-pasquale andrea-pasquale left a comment

Choose a reason for hiding this comment

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

Thanks for implementing this @stavros11.
Everything looks good to me. Good job!

@wilkensJ
Copy link

The unitary() function of a circuit object does not work anymore if a measurement is added.

from qibo import gates, models
circuit = models.Circuit(1)
circuit.add(gates.X(0))
print(circuit.unitary())
# array([[0.+0.j, 1.+0.j],
#        [1.+0.j, 0.+0.j]])
circuit.add(gates.M(0))
print(circuit.unitary())

raises

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nfs/users/jadwiga.wilkens/qibo/src/qibo/models/circuit.py", line 902, in unitary
    return fgate.asmatrix(backend)
  File "/nfs/users/jadwiga.wilkens/qibo/src/qibo/gates/special.py", line 102, in asmatrix
    return backend.asmatrix_fused(self)
  File "/nfs/users/jadwiga.wilkens/qibo/src/qibo/backends/numpy.py", line 117, in asmatrix_fused
    gmatrix = gate.asmatrix(self)
  File "/nfs/users/jadwiga.wilkens/qibo/src/qibo/gates/measurements.py", line 140, in asmatrix
    raise_error(
  File "/nfs/users/jadwiga.wilkens/qibo/src/qibo/config.py", line 46, in raise_error
    raise exception(message)
NotImplementedError: Measurement gates do not have matrix representation.

@scarrazza scarrazza added this to the Qibo 0.2.0 milestone Nov 11, 2022
@stavros11
Copy link
Member Author

The unitary() function of a circuit object does not work anymore if a measurement is added.

Thanks for reporting this. It was fixed and tested in the latest push.

@stavros11 stavros11 removed the do not merge Blocked by some other development or PR label Dec 6, 2022
@scarrazza scarrazza added the enhancement New feature or request label Dec 6, 2022
@scarrazza scarrazza merged commit ca74c80 into master Dec 7, 2022
@stavros11 stavros11 deleted the measurements branch December 7, 2022 09:57
@stavros11 stavros11 mentioned this pull request Dec 7, 2022
4 tasks
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.

4 participants