4
$\begingroup$

Is there a way in Qiskit to initialize $n$ qubits with binary values (0s and 1s)? For example, how can I initialize two qubits in the state $|11\rangle$? Here is a code snippet:

from qiskit import QuantumCircuit
import numpy as np

n = 2
circuit = QuantumCircuit(n, n)

# Seeking some sort of initialization routine like this
circuit.initializeQubits(initialState=np.ones(n), ...)   

# Define rest of the circuit
...

I am aware of the method in this tutorial, which is also referenced here and here. This method creates an arbitrary qubit state by defining an $N$ dimensional ($N = 2^n$) state vector of amplitudes. The problem with this method is it requires creating a state vector which is exponentially large. I'm trying to initialize the qubits by defining an $n$ dimensional binary vector, which for the above example would be [1, 1].

$\endgroup$
1
  • $\begingroup$ Are you looking for a state in superposition? Or is the assumption that the state is a basis state? $\endgroup$
    – C. Kang
    Commented Aug 7, 2020 at 4:15

5 Answers 5

5
$\begingroup$

You can also create a Statevector, that can be directly initialized as follows:

from qiskit.quantum_info import Statevector
sv = Statevector.from_label('11')

You can use sv.evolve(qc) to apply an operator/circuit to the state, where qc is the operator/circuit. sv.data gives you the numpy array, containing the actual implementation of the state.

Check this for more details.

$\endgroup$
7
$\begingroup$

qiskit-terra 0.16 or lower

As answered, probably the most canonical way to do this is with Statevector.from_label and initialize.

Here is the full example:

from qiskit import *
from qiskit.quantum_info import Statevector

n = 2
qc = QuantumCircuit(n)

qc.initialize(Statevector.from_label('1'*n).data, range(n))
qc.draw()
     ┌──────────────────────┐
q_0: ┤0                     ├
     │  initialize(0,0,0,1) │
q_1: ┤1                     ├
     └──────────────────────┘

You could confirm the result like this:

qc.measure_all()
execute(qc, backend=BasicAer.get_backend('qasm_simulator')).result().get_counts()

qiskit-terra 0.17 or higher

This questions inspired a new way to initialize a qubits in the basis states of the Pauli eigenstates Z, X, Y (à la Statevector.from_label).

from qiskit import QuantumCircuit
from qiskit.extensions.quantum_initializer.initializer import Initialize

circuit = QuantumCircuit(6)
circuit.append(Initialize("10+-lr"), range(6))
circuit.draw()
     ┌──────────────────────────┐
q_0: ┤0                         ├
     │                          │
q_1: ┤1                         ├
     │                          │
q_2: ┤2                         ├
     │  initialize(1,0,+,-,l,r) │
q_3: ┤3                         ├
     │                          │
q_4: ┤4                         ├
     │                          │
q_5: ┤5                         ├
     └──────────────────────────┘

The decomposition of this gate is the following:

circuit.decompose().draw()
          ┌───┐ ┌───┐ 
q_0: ─|0>─┤ H ├─┤ S ├─
          ├───┤┌┴───┴┐
q_1: ─|0>─┤ H ├┤ SDG ├
          ├───┤└┬───┬┘
q_2: ─|0>─┤ X ├─┤ H ├─
          ├───┤ └───┘ 
q_3: ─|0>─┤ H ├───────
          └───┘       
q_4: ─|0>─────────────
          ┌───┐       
q_5: ─|0>─┤ X ├───────
          └───┘       
$\endgroup$
3
  • 1
    $\begingroup$ Inspired by this question, I submitted this issue to Qiskit github.com/Qiskit/qiskit-terra/issues/5134 Y'all welcomed to dump thoughts there about how a more intuitive initialize API can help. $\endgroup$
    – luciano
    Commented Sep 27, 2020 at 13:38
  • $\begingroup$ Maybe a feature to come? github.com/Qiskit/qiskit-terra/pull/5229 $\endgroup$
    – luciano
    Commented Oct 15, 2020 at 18:19
  • $\begingroup$ github.com/Qiskit/qiskit-terra/pull/5229 was merged and it will be included in qiskit-terra 0.17 $\endgroup$
    – luciano
    Commented Oct 21, 2020 at 18:38
0
$\begingroup$

Qiskit assumes that initially each qubitt is set to the $|0\rangle$ state. So if you have $n$ qubits, the initial state is $|00..0\rangle$. If you want to flip the state of some specific qubits, you have to apply an $X$ gate to each of those specific qubits. For example, the following code sets the $|11\rangle$ state:

qr = QuantumRegister(2)
cr = ClassicalRegister(2)
circuit = QuantumCircuit(qr, cr, name='mycircuit')
circuit.x(qr[0])
circuit.x(qr[1])
$\endgroup$
0
$\begingroup$

may be you can define a function like below

from qiskit import QuantumCircuit
def qubitinitialize(n,n_init):
#n = number of qubits
#n_init = initial state of each qubit
    qc = QuantumCircuit(n)
    for i in n_init:
        if i == 1:
            qc.x(i)
return(qc)

This function will return a circuit with initial state as you provide in n_init

n = 4  #number of qubits 
n_init = [0,0,1,0] #a bit string of qubit initialization we need 
qc_n = qubitinitialize(n,n_init)
qc_n.draw('mpl')

just remember that 0,1,2,3 in the bit string will convert to q3,q2,q1,q0

$\endgroup$
0
$\begingroup$

Aer simulators will support what you're looking for very soon: https://github.com/Qiskit/qiskit-aer/pull/834

$\endgroup$

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.