1
$\begingroup$

I am new to Quantum ML, and I am currently using PennyLane to do the QML activity.

As per this article, total number of features is equal to the total number of qubits. (In the example, they have considered Iris dataset. And as we know Iris dataset has only 4 features) In my case I have more than 800+ features, and I want to use all of them for prediction.

When I am trying to pass all my data as per the above article, I am getting the below error.

n_qubits = len(X_train[0]) #896 is the output of this step
dev_kernel = qml.device("default.qubit", wires=0)

projector = np.zeros((2**n_qubits, 2**n_qubits))
projector[0, 0] = 1

@qml.qnode(dev_kernel)
def kernel(x1, x2):
    """The quantum kernel."""
    #AngleEmbedding(x1, wires=range(n_qubits))
    #qml.adjoint(AngleEmbedding)(x2, wires=range(n_qubits))
    #return qml.expval(qml.Hermitian(projector, wires=range(n_qubits)))
    AngleEmbedding(x1, wires=range(4))
    qml.adjoint(AngleEmbedding)(x2, wires=range(4))
    return qml.expval(qml.Hermitian(projector, wires=range(4)))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [22], in <cell line: 3>()
      1 dev_kernel = qml.device("default.qubit", wires=0)
----> 3 projector = np.zeros((2**n_qubits, 2**n_qubits))
      4 projector[0, 0] = 1
      6 @qml.qnode(dev_kernel)
      7 def kernel(x1, x2):

ValueError: Maximum allowed dimension exceeded

If I am changing the value back to 4 (to check the error and functioning of wires), I am getting the below error.

ValueError: Features must be of length 4 or less; got length 896.

Can anyone please help me to proceed further. I want to use all my features to create quantum circuit(s). How can I embed all these 896 features with in the available quantum circuits/dimensions.

Thanks in advance!

$\endgroup$

1 Answer 1

5
$\begingroup$

You are asking numpy to initialize an array with dimension greater than $2^{896} \approx 5 \times 10^{269}$. For reference, it is thought that there are maybe $10^{82}$ or so atoms in the observable universe. You could not allocate np.zeros(2**896) in memory even if you gathered a trillion trillion of our universes together and then converted every atom therein into a classical bit of memory.

Therefore, it is impossible to classically simulate a quantum machine learning application where you use angle encoding on 896 features followed by arbitrary unitary operations. If such a task turns out to be useful, it will suggest an example where only a quantum machine learning model could have an advantage over classical learners. However, it is unclear that angle encoding will ever lead to a quantum model that outperforms other much more efficient classical algorithms.

Possible alternatives:

  • Use amplitude encoding, where for each input vector $\mathbf{x} \in \mathbb{R}^{896}$ you prepare the state $|\mathbf{x}\rangle \propto \sum_i x_i |i\rangle$ using $\lceil \log_2 (896)\rceil = 10$ qubits.
  • Use some denser form of angle encoding: Instead of a single rotation $R_x(x_i)$ for each feature $x_i$, you could encode two features on the same qubit like $R_x(x_i) R_y(x_{i+1})$, reducing the number of qubits to half the number of features. You could introduce entangling layers and continue to encode even more features locally.
  • You could apply preprocessing steps so that some transformation of $\mathbf{x}$ is processed by the quantum computer. Its very common in this situation to use PCA for dimensionality reduction, e.g. reduce your 896-dimensional vectors to something with less than 20 dimensions using PCA.fit_transform from scikit-learn, and then use angle encoding on the preprocessed vector. As an extreme example, a method like data-reuploading can work with very large inputs using a single qubit. With either of these techniques, you should be cautious about how much of the difficulty of learning is being offloaded to classical preprocessing steps (possibly making the quantum model's task trivial).
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.