# Create a 2-qubit quantum circuit with given state probabilities

Let's say we are given two probability values $$p_1 > 0$$ and $$p_2 > 0$$, with $$p_1+p_2 \leq 1$$ but not necessarily equals to $$1$$. We are asked to create a 2-qubit quantum circuit with state $$|01\rangle$$ probability as $$p_1$$ and state $$|10\rangle$$ probability as $$p_2$$. How to approach this type of problems? I think entanglement would help here but not sure how to come up with exact probabilities.

Also, is there a way to generalize this (e.g., build a n-qubit quantum circuit from given $$k$$ probabilities exactly for $$k$$ out of its $$2^n$$ superposition states)?

• Do you mean your qubits are initialized in the all $|0\rangle$ state? May 6 at 22:29
• Yes I assumed that pre-condition May 7 at 17:59

Qiskit has a function which initializes a circuit to a certain given state:

https://qiskit.org/documentation/stubs/qiskit.extensions.Initialize.html

The paper that they use to implement the code apparently is:

https://arxiv.org/abs/quant-ph/0406176v5

A small example of code to implement the function is:


from qiskit.circuit import QuantumCircuit
from qiskit.circuit import QuantumRegister

nbqbits = 2 # Number of qubits

qr = QuantumRegister(nbqbits)
qc = QuantumCircuit(qr)

##In psi_0 you define the amplitudes of the state. These amplitudes are the square root
## of the probabilites you want to get for each state.

## Each state of the computational basis binary(i) get the amplitude psi_0[i], i.e:
## The state |00> get the amplitude psi_0[0]
## The state |01> get the amplitude psi_0[1]
## and so on.

psi_0 = [1, 2, 3, 4] # This state is not yet normalized

psi_0 = psi_0/np.linalg.norm(psi_0) # Normalization of the state

qc.initialize(psi_0) # Initialize function. Make sure that the vector you enter has 2**nbqbits entries.

qc.draw(output='mpl')


You should get something like:

d_c = qc.decompose().decompose().decompose().decompose()

d_c.draw(output='mpl')


and you would get:

• Thanks, so in order to obtain the same result, we could have the initialization as psi_0 = [np.sqrt(0.1), np.sqrt(0.3), np.sqrt(0.6), 0] May 7 at 17:58

With qiskit I could solve the first part for a 2-qubit quantum circuit:

from math import asin, sqrt
from qiskit import QuantumCircuit, Aer, execute
from qiskit.visualization import plot_histogram

p1, p2 = 0.3, 0.6 # given probabilities

def prob_to_angle(prob):
return 2*asin(sqrt(prob))

backend = Aer.get_backend('statevector_simulator')
qc = QuantumCircuit(2)

# Apply prior to qubit 0
qc.ry(prob_to_angle(p1), 0)

# Apply prior to qubit 1
qc.x(0)
qc.cry(prob_to_angle(p2/(1-p1)), 0, 1)
qc.x(0)

# execute the qc
results = execute(qc,backend).result().get_counts()

# plot the results
plot_histogram(results)