# Rotating a qubit using QML with Blueqat

In this article, we will be looking into qubit rotation or rotating a |0> state to |1> state using QML optimization where we optimize the parameters of the Rotation operators Rx and Ry in order to achieve the state |1>.

We will be using Blueqat SDK on quantumcomputing.com (Strangeworks) which is a cloud based quantum computing service.

You can also work out this using Blueqat SDK on the local machine. You can install blueqat using python with the pip command :

pip3 install blueqat


The task is to rotate |0> state to |1> state. We will be using Rx and Ry as ansatz to explore the state space on the Bloch Sphere.

To get started we first import the libraries needed for the program :

import numpy as np
from blueqat import Circuit
from scipy.optimize import minimize
from strangeworks.blueqat import StrangeworksBackend


Next, we will be writing the function for the state preparation which is done by using Rx and Ry rotation operators as ansatz.

def state_prep(state,params):
state.rx(params[0])[0]
state.ry(params[1])[0]
return state


We use the criterion of expectation value in order to rotate the qubit to |1> state. Thus we take the expectation value as the cost function which needs to be minimized in order to achieve the state |1>. For this, we measure the expectation value of σᴢ operator or do the measurement in the computational basis. We know that the expectation value of σᴢ operator lies in [-1,1] with the minimum expectation value for the state |1> i.e -1.

Thus, we try to optimize the parameters of both the ansatz to achieve the minimum expectation value i.e -1 in order to achieve the rotated state. The function for the cost value or the expectation value is:

def exp(params):
state=Circuit()
state_prep(state,params)
state.m[:]
shots=1000
b=state.run(shots=shots)
exp_value=(b['0']-b['1'])/shots
return exp_value


Next we try to optimize the above parameters using the Scipy.optimize minimum function with initial parameters as [0.1,0.2] and the learning rate being 0.05.

init_params=np.array([0.1,0.2])
result=minimize(exp,init_params, method="Powell" , tol=0.05)
print(f'The expectation value after optimization is {result.fun}')


Thus the results for the states and params on each iteration are:

Counter({'0': 983, '1': 17})[0.1, 0.2]
Counter({'0': 992, '1': 8})[0.1, 0.2]
Counter({'0': 730, '1': 270})[1.1, 0.2]
Counter({'1': 960, '0': 40})[2.718034, 0.2     ]
Counter({'0': 790, '1': 210})[5.33606803, 0.2       ]
Counter({'1': 949, '0': 51})[2.718034, 0.2     ]
Counter({'1': 933, '0': 67})[2.718034, 0.2     ]
Counter({'1': 655, '0': 345})[2.718034, 1.2     ]
Counter({'1': 553, '0': 447})[ 2.718034, -1.418034]
Counter({'1': 954, '0': 46})[2.718034, 0.2     ]
Counter({'1': 925, '0': 75})[ 2.718034,   -0.41803397]
Counter({'1': 890, '0': 110})[2.718034, 0.581966]
Counter({'1': 968, '0': 32})[ 2.71803400e+00, -8.22658533e-04]
Counter({'0': 788, '1': 212})[ 5.336068,   -0.20164532]
Counter({'1': 949, '0': 51})[ 2.71803400e+00, -8.22658533e-04]
Counter({'0': 783, '1': 217})[ 5.336068,   -0.20164532]
Counter({'0': 507, '1': 493})[-1.51803403,  0.32411523]
Counter({'1': 957, '0': 43})[ 2.71803400e+00, -8.22658533e-04]
Counter({'0': 706, '1': 294})[1.10000004, 0.12329257]
Counter({'1': 915, '0': 85})[ 3.71803397, -0.07753009]
Counter({'1': 999, '0': 1})[ 3.11343772 -0.03115306]
Counter({'1': 998, '0': 2})[ 3.11343772 -0.03115306]
Counter({'1': 778, '0': 222})[3.11343772 0.96884694]
Counter({'0': 531, '1': 469})[ 3.11343772 -1.64918706]
Counter({'1': 1000})[ 3.11343772, -0.03115306]
Counter({'1': 918, '0': 82})[ 3.11343772, -0.64918704]
Counter({'1': 975, '0': 25})[3.11343772 0.35081294]
Counter({'1': 1000})[ 3.11343772, -0.01112575]
Counter({'1': 997, '0': 3})[3.11343772, 0.0890108 ]
Counter({'1': 1000})[ 3.11343772, -0.01112575]
Counter({'1': 955, '0': 45})[ 3.50884144, -0.04145615]
Counter({'1': 892, '0': 108})[2.47366106, 0.03794987]
Counter({'1': 999, '0': 1})[ 3.11343772, -0.01112575]
Counter({'1': 978, '0': 22})[2.86906479, 0.00761947]
Counter({'1': 999, '0': 1})[ 3.2644685,  -0.02271093]
Counter({'1': 999, '0': 1})[ 3.16360143, -0.01497368]
The expectation value after optimization is -1.


We can see the state transitioning from |0> to |1> on the histogram in the above diagrams on quantumcomputing.com (Strangeworks) and also see the change in parameters in the circuit diagram above.

Thus we see the rotation of a qubit from |0> to |1> using expectation value as the cost function and optimizing the parameters for the ansatz.