Check Qiskit documentation Summary of Quantum Operations
under Arbitrary initialization. The function initialize
does what you need (function documentation here).
Example:
from qiskit import QuantumCircuit
import math
desired_vector = [1/math.sqrt(2),-1/math.sqrt(2)]
qc = QuantumCircuit(1) #circuit with 1 qubit
qc.initialize(desired_vector, [0]) #0 in the index of the qubit
will initialize the qubit to the state
[1/math.sqrt(2),-1/math.sqrt(2)]
you can check by doing
from qiskit import execute, Aer
simulator=Aer.get_backend('statevector_simulator')
job = execute(qc, simulator)
qc_state = job.result().get_statevector(qc)
print(qc_state)
If you want the unitary matrix, you can use the unitary_simulator
, in the following way
usimulator=Aer.get_backend('unitary_simulator')
job = execute(qc, usimulator)
umatrix = job.result().get_unitary(qc,decimals=3) #decimals is not necessary
print(umatrix)
which provides
[[ 0.707+0.j -0.707+0.j]
[-0.707-0.j -0.707-0.j]]
Alternative solution
As any state $|b\rangle$ is written as
$$|b\rangle=\cos(\theta/2)|0\rangle + e^{i\varphi}\sin(\theta/2)|1\rangle$$
(global phase does not matter), if you know $\theta$ and $\varphi$, you can easily construct this state by applying $P(\varphi)R_y(\theta)|0\rangle$, where $P(\varphi)$ is a phase gate and $R_y$ is a rotation gate in the $y$-axis.
In Qiskit, this can be done as follows
from qiskit import QuantumCircuit
import math
theta=math.pi/2
phi=0
qc = QuantumCircuit(1) #circuit with 1 qubit
qc.ry(theta,0)
qc.p(phi,0)
The unitary of such operation is straightforward:
$$\begin{bmatrix}\cos(\theta/2) & -\sin(\theta/2) \\ \sin(\theta/2)e^{i\varphi} & \cos(\theta/2)e^{i\varphi} \end{bmatrix}.$$
Such a gate can also be implemented directly with Qiskit native gate U3 (with $\lambda=0$).