Error when increasing Qubits number in QML Demos

Hi, I’m getting an error when increasing the number of Qubits in this QML demo:
Quantum detection of time series anomalies | PennyLane Demos
It’s supposed to perform signal anomaly detection, but it appears it only works with “n_qubits”: 1. Although the code treats “n_qubits” as a variable that can be trivially changed.
When changing to “n_qubits”: 2, it returns the error: TypeError: Object ‘NoneType’ is not subscripted.
Does anyone know what I should adapt in the code?
“n_qubits” can be changed in “general_options”.

My opinion: I’ve run this a few times and noticed that this type of error is related to missing input values in some function, as the “Covalent” module creates a workflow, and when it doesn’t match, it results in “none”. Therefore, the value is none in the returned variable. Therefore, some function is only suitable for 1 qubit.

Hey @Victor_Meneguitte! Welcome to the forum :sunglasses:

If I’m understanding correctly, there are two places where n_qubits is “hardcoded”:

  1. the second codeblock in the section Building the loss function has
n_qubits = 1
dev = qml.device("default.qubit", wires=n_qubits, shots=None)
D_one_qubit = qml.qnode(dev)(D)
_ = qml.draw_mpl(D_one_qubit, decimals=2)(torch.tensor([1, 0]), 1, 1, True)
  1. In general_options:
general_options = {
    "n_qubits": 1,

When I change the value in general_options, I get the error you’re seeing. Is this exactly what’s going on for you?

1 Like

Yes @isaacdevlugt , the error occurs precisely when changing the value in “general_options”.

Glad I understand :slight_smile:. I reached out to one of the authors and I will get back to you as soon as I hear back :+1:

Hey @Victor_Meneguitte, just heard back from one of the authors:

So it seems that this demo was indeed hardcoded for one qubit despite having an input option for n_qubits . The reason is that it is hard to generalize the correct input dimensions for the PL templates. An example with n_qubits is available in the papers with code demo of this paper (which is on the PL community demos). Github repo is here: GitHub - AgnostiqHQ/QuantumVariationalRewinding: Covalent demonstration of the QVR algorithm using a cryptocurrency time series use case

Let me know if this helps!

Hi @isaacdevlugt it seems like this other GitHub you mentioned is hardcoded for n_qubits=2 this time, as in line 5 has the comment as follows:
“Work explicitly with 2 qubits. This corresponds to the ‘bivariate model’ in the article”
n_qubits = 2
Is this the case or did i misunderstand the code?

Hi @Victor_Meneguitte,
Did you try the code in the GitHub shared by Isaac for n_qubits>2?
From Isaac’s answer I understand that it should work. However if this isn’t working at the moment please let us know and we can seek further clarification with one of the authors.

Hey @Victor_Meneguitte! Sorry about this. The person I spoke with will take a look at what’s going on and get back to me soon. I’ll get back to you early next week at the latest!

Yes, @CatalinaAlbornoz , it doesn’t work by simply changing the n_qubits variable, but the rest of the code does indeed seem to be generalized to n_qubits. Maybe there is some other variable in the training options that needs to be changed to match 3 qubits or n qubits.
Ok, @isaacdevlugt .Thank you both!

1 Like

Hey @Victor_Meneguitte! Apologies for the delay here. We haven’t forgotten about this post and will do our best to get back to you before the end of the week :slight_smile:.

@Victor_Meneguitte thanks for your patience!

Using the notebook on the GitHub repository, a simple way to use a general n_qubits is to:

  1. In code cell 5, set the variable n_qubits to whatever you wish.
  2. In code cell 16, set n_qubits to the same value you set in code cell 5 and set num_distributions to 2**n_qubits - 1 . The code should now run for any 1<=k<=n_qubits .

The logic behind step 2 is that in code cell 6, we are creating a parameterized diagonal unitary with a number of parameters equal to num_distributions . In the case of a fully connected Hamiltonian (k==n_qubits), in the existing code, we require 2^n_qubits -1 parameters. If you set k lower, you can get away with setting num_distributions lower too. If you set num_distributions=2^n_qubits - 1 and set 1<=k<n_qubits , the code will still work using only a subset of the 2^n_qubits -1 parameters to build the diagonal unitary.

It’s also worth noting that Barren Plateaus are going to start appearing when qubit numbers are ramped up (as is the case for many QML algos dealing with classical data). The circuit ansatze used don’t have inductive bias for this problem.

Let us know if that helps!

1 Like

Hi @isaacdevlugt , it seems like you nailed the problem, the code on the GitHub QuantumVariationalRewinding/QVR_example.ipynb at main · AgnostiqHQ/QuantumVariationalRewinding · GitHub is fully generalized for n_qubits now.

In fact, I observed that even the previously mentioned code from the demos tutorial Quantum detection of time series anomalies | PennyLane Demos can be run with n_qubits too. In that case, the variable gamma_length must be settled equals to 2^n_qubits - 1. while 1<=k<=n_qubits still.

It was very helpful, thank you very much! :blush:

1 Like

Awesome! Glad we could help :slight_smile: