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

Reuploading classifier expects states method from circuit result object #1286

Open
sorewachigauyo opened this issue Mar 28, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@sorewachigauyo
Copy link
Contributor

Describe the bug
The reuploading classifier example expects a state() method for the circuit result object on line 85 of qclassifier.py. This method exists for the QuantumState object which is a result of the numpy backend, but the qibolab backend returns a MeasurementOutcomes object which does not have said method.

To Reproduce

cd qibo/examples/reuploading_classifier
export QIBO_BACKEND="qibolab"
export QIBO_PLATFORM="dummy"
python3 main.py

On numpy

Problem solved before, obtaining parameters from file...
------------------------------------------------------------
[Qibo 0.2.5|INFO|2024-03-28 09:43:48]: Using numpy backend on /CPU:0
The value of the cost function achieved is 0.015078
The accuracy for this classification is 88.90 %

On qibolab

Problem solved before, obtaining parameters from file...
------------------------------------------------------------
[Qibo 0.2.5|INFO|2024-03-28 11:45:57]: Loading platform dummy
[Qibo 0.2.5|INFO|2024-03-28 11:45:57]: Using qibolab (dummy) backend on /CPU:0
[Qibo 0.2.5|ERROR|2024-03-28 11:45:57]: ry is not a native gate.
[Qibo 0.2.5|INFO|2024-03-28 11:45:57]: Connecting to instrument <qibolab.instruments.dummy.DummyInstrument object at 0x000002AADC03AE10>.
[Qibo 0.2.5|INFO|2024-03-28 11:45:57]: Connecting to dummy instrument.
[Qibo 0.2.5|INFO|2024-03-28 11:45:57]: Connecting to instrument <qibolab.instruments.dummy.DummyLocalOscillator object at 0x000002AADC049090>.
[Qibo 0.2.5|INFO|2024-03-28 11:45:57]: Minimal execution time (sequence): 0.0008
Traceback (most recent call last):
  File "C:\Users\pault\Documents\repos\qibo\examples\reuploading_classifier\main.py", line 53, in <module>
    main(**args)
  File "C:\Users\pault\Documents\repos\qibo\examples\reuploading_classifier\main.py", line 45, in main
    value_loss = ql.cost_function_fidelity()
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\pault\Documents\repos\qibo\examples\reuploading_classifier\qlassifier.py", line 104, in cost_function_fidelity
    cf += self.cost_function_one_point_fidelity(x, y)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\pault\Documents\repos\qibo\examples\reuploading_classifier\qlassifier.py", line 85, in cost_function_one_point_fidelity
    state = C.execute().state()
            ^^^^^^^^^^^^^^^^^
@sorewachigauyo sorewachigauyo added the bug Something isn't working label Mar 28, 2024
@scarrazza
Copy link
Member

@stavros11 besides this issue, could you please send to @sorewachigauyo our first notebook for quantum vqc regression, presented years ago to the TII team? Otherwise @MatteoRobbiati could you share the code used for the pdf fit? Thanks.

@MatteoRobbiati
Copy link
Contributor

MatteoRobbiati commented Mar 28, 2024

Thanks for opening this @sorewachigauyo.
The reuploading classifier, according to the algorithm introduced in the original paper needs the state representation to compute the loss function, which is a state fidelity indeed.

If you consider a more hardware-friendly execution, e.g. shot-noise simulation or, indeed, execution on some quantum device, this strategy cannot be applied. In fact, what we can use to process information (at least without the gate set tomography) are only the frequencies we get after measurements.

For this reason, in other works involving reuploading models (like the aforementioned regression), we defined a different optimization strategy, computing a Mean Squared Error loss function and using expectation values as predictors.

An example of this approach can be found in a recent code we implemented to compute real time quantum error mitigation during a VQA.

To give you a practical example:
In the case of a binary classification one can use a Pauli $Z$ as target observable and compute $$f = \langle 0 | U^{\dagger} Z U | 0\rangle,$$ where $U$ is your variational circuit.
Then classify the point as class A if $f\leq0.5$ or class B if $f&gt;0.5$.

The expressibility of the model remains untouched, since you are only changing the way of processing the final information.

In the end, I would say this error is well expected if the adopted strategy is the one implemented in the qclassifier.

@stavros11
Copy link
Member

@stavros11 besides this issue, could you please send to @sorewachigauyo our first notebook for quantum vqc regression,

In case this is still helpful, I uploaded this notebook here: https://gist.github.com/stavros11/d737f45ef816911e1d40d0a0fd0fcce3
From a quick look it seems to only be using Circuits so it should still work, despite all the changes that have been done in qibolab since that time (the only issue may be the transpiler/native gates). However, this notebook was just for a very first demo and the problem presented is quite simple, so the code shared by @MatteoRobbiati may be more useful.

@marekgluza
Copy link
Contributor

Thanks @stavros11 and @MatteoRobbiati !

In fact, what we can use to process information (at least without the gate set tomography) are only the frequencies we get after measurements.

The qubit state can be obtained as
$$\rho = \frac 12 (I + \langle X\rangle_\rho X+ \langle Y\rangle_\rho Y+ \langle Z\rangle_\rho Z$$
so the qibolab backend could have this as a simple (and noisy) way of returning the single qubit state

@mho291 are you planning to add such very simple procedures to test GST? If not, then @scarrazza we should plan to have more simple procedures in place for the 20q iqm chip?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants