In qiskit, how can I initialise a qubit in a complex state, specifically in the state: $$\left|q\right> = \frac{1}{\sqrt2} \left|0\right> + i \frac{1}{\sqrt2} \left|1\right>$$
2 Answers
You can generate a general quantum state $|\psi\rangle$ in Qiskit by using their Custom package.
from qiskit.aqua.components.initial_states import Custom
import math
state_vector = [1, 1j]
Psi = Custom(1,state_vector = state_vector).construct_circuit()
Psi.draw()
Running the above code in Qiskit would give you something like below:
But also notice that your state $|q\rangle = \dfrac{1}{\sqrt{2}}\big( |0\rangle + i | 1\rangle \big) $ is actually resulting from
$$ \dfrac{Z + Y}{\sqrt{2}} |0\rangle = |q\rangle $$
where $Z$ and $Y$ are the Pauli matrices and $|0\rangle = \begin{bmatrix} 1 \\ 0 \end{bmatrix} $.
You can use initialize
function. Here is a code constructing your one qubit state:
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, Aer, execute
import math as m
quantumState = [
1 / m.sqrt(2) * complex(1, 0),
1 / m.sqrt(2) * complex(0, 1)]
q = QuantumRegister(1, name = 'q')
c = ClassicalRegister(1, name = 'c')
circuit = QuantumCircuit(q,c)
circuit.initialize(quantumState, [q[0]])
You can prepare multiqubit states as well, for example for $|\psi\rangle = \frac{1}{2}(|00\rangle + |01\rangle+|10\rangle+|11\rangle)$:
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit, Aer, execute
import math as m
quantumState = [0.5,0.5,0.5,0.5]
q = QuantumRegister(2, name = 'q')
c = ClassicalRegister(2, name = 'c')
circuit = QuantumCircuit(q,c)
circuit.initialize(quantumState, [q[0],q[1]])