6

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

luciano
  • 5,763
  • 1
  • 12
  • 34
Vedo
  • 187
  • 7

1 Answers1

4

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

luciano
  • 5,763
  • 1
  • 12
  • 34
  • 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 – Vedo May 26 '21 at 13:20
  • I extended the answer based on your comment. Is it answering your question now? – luciano May 26 '21 at 13:29
  • 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 – Vedo May 26 '21 at 13:30
  • new attempt. TL;DR, your backend does not support initialize and you have to transpile your circuit first. – luciano May 26 '21 at 13:43
  • Thank you it works :) – Vedo May 26 '21 at 14:00
  • @Don I don't think they will allow you to initialize the state (2,0) like you commented. It will probably throw you an error about the vector not being a unit vector. Similar to the way qiskit will throw you an error if you try to create a gate from a matrix and your matrix is not unitary... – KAJ226 May 26 '21 at 17:33
  • @luciano I like this array_to_latex feature that you guys have included in your visualization package. Very nice. – KAJ226 May 26 '21 at 17:35