# Setting initial state in Qiskit unitary simulator

I'm getting started in IBM quantum lab for quantum computing. My task is to put quantum state $$|0\rangle$$ on the 1st qubit and state $$|1\rangle$$ on second one. I tried using this method to initialize it as:

qc.initialize(0,0)
qc.initialize(1,1)


but when I run in the unitary simulator, I get the following error:

Simulation failed and returned the following error message:
ERROR: Failed to load qobj: Invalid qobj "initialize" instruction ("params" is incorrect length).


I also tried using reset method but it displayed error as well

qc.reset(0)
qc.reset(1)
qc.x(1)


If anyone knows how to set state to a specific qubit in Qiskit it would be great, thanks in advance

The full code:

#Creating a quantum circuit with two qubits
qc=QuantumCircuit(2)

#Set the state of the first qubit to|0⟩ and set the state of the second qubit to|1⟩.
qc.initialize(0,0)
qc.initialize(0,0)
#or
#qc.reset(0)
#qc.reset(1)
#qc.x(1)

qc.h(0)
qc.h(1)

#Applying CNOT operator, where the controller qubit is the first qubit and the target qubit is the second qubit
qc.cx(0,1)

qc.h(0)
qc.h(1)

display(qc.draw())
usim = Aer.get_backend('unitary_simulator')
qobj = assemble(qc)
unitary = usim.run(qobj).result().get_unitary()
array_to_latex(unitary, pretext="\\text{Circuit = }\n")

• It would be helpful to see the full code leading to the error. May 26 at 13:27
• ok I'll post it rn
– Don
May 26 at 13:28

If I understand correctly, your circuit looks like this:

from qiskit import *
qc = QuantumCircuit(2)
qc.initialize(0,0)
qc.initialize(1,1)

qc.h(0)
qc.h(1)
qc.cx(0,1)
qc.h(0)
qc.h(1)

qc.draw('mpl') And you want to run it in the Aer unitary simulator. However, the simulator does not support the instruction initialize. So you need to transpile your circuit to the instruction set of the selected backend. You can do that with transpile:

usim = Aer.get_backend('unitary_simulator')
transpiled = transpile(qc, backend=usim)
transpiled.draw('mpl') Once transpiled, you can run it in the backend:

qobj = assemble(transpiled)
unitary = usim.run(qobj).result().get_unitary()


Here is the resulting unitary:

from qiskit.visualization import array_to_latex
array_to_latex(unitary, prefix="\\text{Circuit = }\n") • pretty much, but worst part is how do I do this then: We start in quantum state|01⟩. What is the outcome? Because when initializing I can literally put (2,0) and I will get state 2 which is not possible I think
– Don
May 26 at 13:20
• new attempt. TL;DR, your backend does not support initialize and you have to transpile your circuit first. May 26 at 13:43