Variational classifier

Hi everyone, I have data that has 5 features and 1 label. This features like array([ 0.414783, 0.815285, -4.813031, 0.266482, 2.301442, 0.284654],
dtype=float32)
I have tried use this Variational classifier — PennyLane documentation demo but I’m confused while build the variational circuit.
** dev = qml.device(“default.qubit”, wires=2)**
How many wires/qubit should I use for 5 features and 1 label?

While build layer method, what shape weights_init matrix num_qubits, num_layers, etc?

num_layers = 2
num_qubits = 4
weights_init = 0.01 * np.random.randn(num_layers, num_qubits, 3, requires_grad=True)

I guess, I’m right the layer method should be above for 5 features and 1 label,

dev = qml.device(“default.qubit”, wires=5)

def layer(W):

qml.Rot(W[0, 0], W[0, 1], W[0, 2], wires=0)
qml.Rot(W[1, 0], W[1, 1], W[1, 2], wires=1)
qml.Rot(W[2, 0], W[2, 1], W[2, 2], wires=2)
qml.Rot(W[3, 0], W[3, 1], W[3, 2], wires=3)
qml.Rot(W[4, 0], W[4, 1], W[4, 2], wires=4)

qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[1, 2])
qml.CNOT(wires=[2, 3])
qml.CNOT(wires=[3, 4])
qml.CNOT(wires=[4, 0])

Ok, can you help me? thank you so much :pray:

Hi @GONUL_SABAH,

You’re right.

First, classical features should be embedded in qubits state.
Common Data emmbedding schemes such as angle embedding and basis embedding require N qubits to embedd N features.
For instance, if N is 5,

# classical data , x , has 5 features.
# Angle-embedding by qml.RX

qml.RX(x[0],wires=0)
qml.RX(x[1],wires=1)
qml.RX(x[2],wires=2)
qml.RX(x[3],wires=3)
qml.RX(x[4],wires=4)

Note: another approach of data emmbedding is amplitude encoding in which only log2(N) qubits are required to embedd N features.
However, amplitude encoding requires huge gate counts.

Then, the states are scrambled by parameterized gates.

qml.Rot(W[0, 0], W[0, 1], W[0, 2], wires=0)
qml.Rot(W[1, 0], W[1, 1], W[1, 2], wires=1)
qml.Rot(W[2, 0], W[2, 1], W[2, 2], wires=2)
qml.Rot(W[3, 0], W[3, 1], W[3, 2], wires=3)
qml.Rot(W[4, 0], W[4, 1], W[4, 2], wires=4)

qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[1, 2])
qml.CNOT(wires=[2, 3])
qml.CNOT(wires=[3, 4])
qml.CNOT(wires=[4, 0])

Ofcourse, you also can truncate some paramterized gates although the expressivity will drop.

qml.Rot(W[0, 0], W[0, 1], W[0, 2], wires=0)
qml.Rot(W[1, 0], W[1, 1], W[1, 2], wires=2)
qml.Rot(W[2, 0], W[2, 1], W[2, 2], wires=4)

qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[2, 3])
qml.CNOT(wires=[4, 0])

Moreover, “repeat” is welcome to enhance the expressivity.

# two variational layers!

# 1st
qml.Rot(W[0, 0], W[0, 1], W[0, 2], wires=0)
qml.Rot(W[1, 0], W[1, 1], W[1, 2], wires=1)
qml.Rot(W[2, 0], W[2, 1], W[2, 2], wires=2)
qml.Rot(W[3, 0], W[3, 1], W[3, 2], wires=3)
qml.Rot(W[4, 0], W[4, 1], W[4, 2], wires=4)

qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[1, 2])
qml.CNOT(wires=[2, 3])
qml.CNOT(wires=[3, 4])
qml.CNOT(wires=[4, 0])

# 2nd

qml.Rot(W[5, 0], W[5, 1], W[5, 2], wires=0)
qml.Rot(W[6, 0], W[6, 1], W[6, 2], wires=1)
qml.Rot(W[7, 0], W[7, 1], W[7, 2], wires=2)
qml.Rot(W[8, 0], W[8, 1], W[8, 2], wires=3)
qml.Rot(W[9, 0], W[9, 1], W[9, 2], wires=4)

qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[1, 2])
qml.CNOT(wires=[2, 3])
qml.CNOT(wires=[3, 4])
qml.CNOT(wires=[4, 0])

Finally, measurement should be performed.
In binary-classifier, the output should be a scaler value.
Therefore, one of 5 qubits is measured.

    return qml.expval(qml.PauliZ(0))

Ofcourse, the position of qubits can be changed if you want.

    return qml.expval(qml.PauliZ(1))

After the measurement, paramterized gates , W, will be updated so that cost (=classification error) is minimized.

Please let me know if you have questions.

2 Likes

Thank you so much :cherry_blossom:
in the tutorial there has get_angles method. when this method run, they use 2 qubits. I guess, this method is used to encode real-valued x/features. So 0.414783, 0.815285, -4.813031, 0.266482, 2.301442, 0.284654] this x need set of angles in my case? How can I build get_angles method for 6 features?
What the purpose this get_angles transfer?

One quick solution is, just using built-in template.

qml.MottonenStatePreparation — PennyLane 0.20.0 documentation

To understand what is done inside the template, we have to refer the paper.

Note that MottonenStatePreparation consumes a large number of gate.
Therefore, the circuit execution migh be slow.

Roughly speaking, the reason why we have to calculate “angle” for state preparation is that state preparation uses the following equation.
cos(arccos(x)) = x
cos( ) can be implemented by using RY-gate.
Therefore, if we get arccos(x) by classical pre-processing, we can embed x in quantum amplitude by RY-gate.

In addition to Mottonen state preparation, another method is an abstract state preparation.
qml.ArbitraryStatePreparation — PennyLane 0.20.0 documentation
In simulations, the abstract state preparation would be useful.

2 Likes