common.title

Docs
Quantum Circuit
TYTAN CLOUD

QUANTUM GAMING


Overview
Terms of service

Privacy policy

Contact
Research

Sign in
Sign up
common.title

Data input/output for continuous values used in quantum machine learning

Yuichiro Minato

2022/03/28 05:35

Data input/output for continuous values used in quantum machine learning

If you think that machine learning using a quantum computer makes it difficult to take continuous values for data input because qubits take 01 discrete values, you will learn how to put in continuous values and take out continuous values as output.

Enter data in continuous value angles

Data is input by angle. The qubit has an arbitrary rotation angle gate, so continuous values are converted to angles.

from blueqat import Circuit
import numpy as np

#Create data with values greater than 0 and less than 1
data = np.random.rand()
data
0.792897389159987

This value is converted to an angle and placed in the RX gate. Specify the angle and then the qubit number.

data2 = data*2*np.pi
c = Circuit().rx(data2)[0]
print(c)
c.run()
Circuit(1).rx(4.981921225671084)[0]
array([-0.79570114+0.j        ,  0.        -0.60568944j])
c.run_with_ibmq(returns='draw', output='mpl')
<Figure size 133.526x144.48 with 1 Axes>

image

This time we checked the state vector and found complex values. Sampling should be measured on the same circuit and the number of runs should be specified.

(c+ Circuit().m[:]).run(shots=100)
Counter({'1': 41, '0': 59})
(c+ Circuit().m[:]).run_with_ibmq(returns='draw', output='mpl')
<Figure size 193.726x144.48 with 1 Axes>

image

In this way, samples could be taken from the circuit from which the data was entered.

Correct state vector and sampling values to expected values

The input is now continuous value. The output can be a continuous value using this state vector. Since the state vector is complex and continuous in nature, it should be easy to take it out. Calculate the expectation value. The expectation value can be created from the state vector and the Hamiltonian. In this case, we will calculate the expectation value of pauli Z since it is one qubit.

The calculation is simple. The formula is E = <\psi|Z|\psi>, which sounds difficult, but in fact, we can just sandwich the matrix Z between the state vectors.

#get the state vector
result = c.run()
print(result)

#expectation value
exptZ = result.conj()@np.array([[1,0],[0,-1]])@result
exptZ.real
[-0.79570114+0.j          0.        -0.60568944j]
0.26628059914597774

Calculation results are now available. Continuous values were retrieved. Of course, you can calculate by hand. The above formula is |\alpha|^2 - |\beta|^2 for the state vector, so

exptZ = np.abs(result[0])**2 - np.abs(result[1])**2
exptZ
0.26628059914597774

The same values were retrieved. Next, let's try sampling. The same formula is used to calculate 10000 times, and 0 and 1 are obtained the desired number of times.

sample = 10000
result = (c + Circuit().m[:]).run(shots=sample)
print(result)
Counter({'0': 6351, '1': 3649})

Calculating the expected value is simple: (number of 0's - number of 1's)/number of samples.

exptZ = (result['0'] - result['1'])/sample
exptZ
0.2702

We got relatively close values. The only way to get close is to do lots of calculations using the law of large numbers. 100000 calculations.

sample = 100000
result = (c + Circuit().m[:]).run(shots=sample)
print(result)

exptZ = (result['0'] - result['1'])/sample
exptZ
Counter({'0': 63116, '1': 36884})
0.26232

I think we are getting a little closer. In the field of quantum machine learning, where accuracy is needed, we often use simulators, so we often use state vectors. When we use real machines, we cannot use state vectors, so let's use sampling. That's all.

© 2025, blueqat Inc. All rights reserved