# How can I create an arbitrary superposition state of two qubits in Qiskit?

I am trying to produce any arbitrary superposition state of two qubits, for example $$a|00\rangle + b|01\rangle + c|10\rangle + d|11\rangle$$, such that $$a^2 + b^2 + c^2 + d^2 = 1$$ . How can I do that with Qiskit?

One way that I have found out is to use $$U_3(\theta, 0, 0)$$ gates for suitable values of $$\theta$$ on both of the qubits. Is there another way?

qiskit.quantum_info has a useful random_statevector function that you can use to generate a random statevector that's sampled from the uniform measure. For a 2-qubit state, you would call it as follows:

from qiskit.quantum_info import random_statevector
state = random_statevector(4) # note: the parameter you pass is the number of entries in the vector, not the number of qubits


Once you have this statevector, the initialize function lets you initialize your qubits to the state you just created. The documentation for initialize is not very helpful so here's how you would use it:

from qiskit import QuantumCircuit
qc = QuantumCircuit(2)
qc.initialize(state.data, [0,1])


Note that you will have to use state.data to get the underlying array representing the statevector, because initialize doesn't accept an argument of type StateVector.

Hope this helps!

Putting my answer here just for completeness. Initialise 2 qubits with random state in a quantum circuit, that's it, you will have what you need. After that you can use statevector_simulator to verify the final superposition state. You can also use plot_bloch_multivector to view it.

I made code a bit verbose for clarity.

Code

from qiskit import QuantumCircuit, Aer, execute
from qiskit.quantum_info.states.random import random_statevector
from qiskit.extensions import Initialize
from qiskit.visualization import plot_bloch_multivector

state_backend = Aer.get_backend('statevector_simulator')

def initrstate(qc, label=""):
state = random_statevector((2,1)).data
print(state) # if you want to see what state you have initialized the qubit to
init = Initialize(state)
init.label = label
return init

def viewstate(qc):
result = execute(qc, state_backend).result()
fstate = result.get_statevector()
print(fstate) # This will be the superposition state
return fstate

qc = QuantumCircuit(2)
init1 = initrstate(qc)
init2 = initrstate(qc)
qc.append(init1, [0])
qc.append(init2, [1])

plot_bloch_multivector(viewstate(qc)) #Plot the state vectors on the bloch sphere


Adding my answer as well to fill out the other suggestions. You can use this to construct a circuit based on an arbitrary state vector: https://qiskit.org/documentation/stubs/qiskit.aqua.circuits.StateVectorCircuit.construct_circuit.html#qiskit.aqua.circuits.StateVectorCircuit.construct_circuit

You initialize StateVectorCircuit with your array of complex values.