6
$\begingroup$

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)

#Applying Hadamard to both qubits
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)

#Applying Hadamard to both qubits
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")
$\endgroup$
2
  • $\begingroup$ It would be helpful to see the full code leading to the error. $\endgroup$ Commented May 26, 2021 at 13:27
  • $\begingroup$ ok I'll post it rn $\endgroup$
    – Vedo
    Commented May 26, 2021 at 13:28

1 Answer 1

4
$\begingroup$

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') 

enter image description here

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')

enter image description here

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")

enter image description here

$\endgroup$
7
  • $\begingroup$ 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 $\endgroup$
    – Vedo
    Commented May 26, 2021 at 13:20
  • $\begingroup$ I extended the answer based on your comment. Is it answering your question now? $\endgroup$
    – luciano
    Commented May 26, 2021 at 13:29
  • $\begingroup$ partially because when I run my code I want to see the measurements but using reset/decompose and initialize method it doesn't allow me, I added my whole code so you can see what I mean $\endgroup$
    – Vedo
    Commented May 26, 2021 at 13:30
  • $\begingroup$ new attempt. TL;DR, your backend does not support initialize and you have to transpile your circuit first. $\endgroup$
    – luciano
    Commented May 26, 2021 at 13:43
  • $\begingroup$ Thank you it works :) $\endgroup$
    – Vedo
    Commented May 26, 2021 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.