How do we create an arbitrary vector of the following form in Qiskit?
Say, we want to initialize a qubit with a vector, $$ \vert \psi \rangle = \frac{1+i}{\sqrt{3}}\vert 0 \rangle - \frac{i}{\sqrt{3}}\vert 1 \rangle $$
If I put it in the form
$$
\vert \psi \rangle = \frac{1+i}{\sqrt{3}}\vert 0 \rangle - \frac{i}{\sqrt{3}}\vert 1 \rangle = cos(\frac{\theta}{2})|0> + e^{i\phi}sin(\frac{\theta}{2})|1>
$$ where
$0 < \theta < \pi$ and $0 < \phi <2\pi$
and then calculate $\theta$ and $\phi$
So, $$ cos(\frac{\theta}{2}) = \frac{1+i}{\sqrt{3}}\\ e^{i\phi}sin(\frac{\theta}{2}) = - \frac{i}{\sqrt{3}} $$ Therefore, $$ \theta = 2 * \arccos{\frac{1+i}{\sqrt{3}}} \\ \phi = i * ln(\frac{- \frac{i}{\sqrt{3}}}{sin(\frac{\theta}{2})}) $$
Now, I am using the following code..
#We create the quantum state manually first
arb_quantum_state = ((1+1.j)/math.sqrt(3))*ket_0 - (1.j/math.sqrt(3))*ket_1
print(arb_quantum_state)
theta = 2*cmath.acos((1+1.j)/cmath.sqrt(3))
print('theta : ',theta)
sinValue = cmath.sin(theta/2)
print(sinValue)
phase = -1*(1.j/cmath.sqrt(3))/sinValue
phi = cmath.log(phase)/1.j
print('phi : ',phi)
# Use these theta and phi to create the circuit
circ = QuantumCircuit(1,1)
#Verify why complex values are not allowed
#circ.u3(theta.real,phi.real,0,0)
circ.u3(theta,phi,0,0)
results = execute(circ, backend=Aer.get_backend('statevector_simulator')).result()
quantum_state = results.get_statevector(circ, decimals=3)
print (quantum_state)
The above code creates the gate alright, but the execute function is returning the following error,
TypeError: can't convert complex to float
However, if I use just the real values of theta and phi, then the execute function returns a state vector, which is different than the one it should be.