# Creating a Qiskit circuit based on output states

I am trying to create a Qiskit circuit that:

• starting in state |00⟩ generates a 1/2 * (|00⟩ - |01⟩ - |10⟩ + |11⟩) state

• starting in state |10⟩ generates a 1/2 * (|00⟩ + |01⟩ - |10⟩ -|11⟩) state

• starting in state |01⟩ transforms to a 1/2 * (|00⟩ -|01⟩ + |10⟩ - |11⟩) state

• starting in state |11⟩ transforms to a 1/2 * (|00⟩ + |01⟩ + |10⟩ + |11⟩) state

• I'm not sure, but it seems that the states $|01\rangle$ and $|10\rangle$ are switched. Any way, my answer below is based on the question as it is now. Apr 16 at 13:41
• This should remind you the quantum Fourier transform without the phases and the 0, 1 states swapped. Therefore the circuit is just (XX)(HH) Apr 16 at 17:30
• – glS
Apr 17 at 10:11

Note that $$H|0\rangle = \dfrac{|0\rangle + |1\rangle}{\sqrt{2}}$$ and $$Z|0\rangle = |0\rangle$$ and $$Z|1\rangle = -|1\rangle$$. So from here you can see that

$$(H\otimes H) |00 \rangle = H|0\rangle \otimes H|0\rangle = \dfrac{|0\rangle + |1\rangle}{\sqrt{2}}\otimes \dfrac{|0\rangle + |1\rangle}{\sqrt{2}} = \dfrac{|00\rangle + |01\rangle + |10\rangle + |11\rangle}{2}$$

then

$$(Z \otimes Z) \dfrac{|00\rangle + |01\rangle + |10\rangle + |11\rangle}{2} = \dfrac{|00\rangle - |01\rangle - |10\rangle + |11\rangle}{2}$$

therefore to create the state $$|\psi \rangle = \dfrac{|00\rangle - |01\rangle - |10\rangle + |11\rangle}{2}$$ from the initial state $$|\psi_0 \rangle = |00\rangle$$ you just need to perform the following circuit:

Similarly, if you start with the state $$|\psi_0 \rangle = |10\rangle$$ and you want to get to the state $$|\psi \rangle = \dfrac{|00\rangle + |01\rangle - |10\rangle - |11\rangle}{2}$$ then you can use the additional fact that $$H|1\rangle = \dfrac{|0\rangle - |1\rangle}{\sqrt{2}}$$ and so

$$(H\otimes H) |10 \rangle = H|1\rangle \otimes H|0\rangle = \dfrac{|0\rangle - |1\rangle}{\sqrt{2}}\otimes \dfrac{|0\rangle + |1\rangle}{\sqrt{2}} = \dfrac{|00\rangle + |01\rangle - |10\rangle - |11\rangle}{2}$$

**Note that the circuit is in little endian convention (read bottom to top).

I will leave the other two cases for you to try.

• Thanks for the answer. It seems that I need to cover four scenarios in one circuit. Is it possible? Apr 16 at 12:35

One way to do that using Qiskit is to write down the unitary matrix which transforms input states to output states then using QuantumCircuit.unitary()[1] method.

The unitary should be $$4\times4$$. First column corresponds to the output of the $$|00\rangle$$ state. Second column corresponds to the output of the $$|01\rangle$$ state. And so on.

Try to do it yourself then verify the result is: \begin{bmatrix} 0.5 & 0.5 & 0.5 & 0.5 \\ -0.5 & -0.5 & 0.5 & 0.5 \\ -0.5 & 0.5 & -0.5 & 0.5 \\ 0.5 & -0.5 & -0.5 & 0.5 \\ \end{bmatrix}

Now create the circuit:

matrix = [
[ 0.5,  0.5,  0.5, 0.5],
[-0.5, -0.5,  0.5, 0.5],
[-0.5,  0.5, -0.5, 0.5],
[ 0.5, -0.5, -0.5, 0.5],
]

# Create the circuit:
circ = QuantumCircuit(2)
circ.unitary(matrix, [0, 1])

# Draw it:
circ.decompose().draw('mpl')


To verify the end result you can simulate it using StatevectorSimulator[2]. However, for small circuits like the one we have here, there is a simpler way:

from qiskit.quantum_info import Statevector
from qiskit.quantum_info.operators import Operator

Statevector.from_label('00').evolve(circ).draw('latex')


You can put 01, 10, or 11 instead of 00 to check the result.