# Qiskit programming: convert qubit to quantum register

When simulating quantum circuits in Qiskit, sometimes you need to build your own operation. And when you are doing so you would like to save the computational cost, but the QuantumCircuit object takes only QuantumRegister object as input(while rejects the qubit object) and this might be cumbersome.

For instance, see the code:

def swappedOp(obj,qr0,qr1,index):
circ=QuantumCircuit(qr0,qr1)
circ.swap(qr0[qr0.__len__()-1],qr1[index])
circ.append(obj,[qr0[i] for i in range(qr0.__len__())]+[qr1[i] for i in range(qr1.__len__())])
circ.swap(qr0[qr0.__len__()-1],qr1[index])
return Operator(circ)


In this code, my input obj is a random unitary, and what this function does is to swap the action of two qubits(in this code, it swaps the last qubit of qr0 and qr1[index]) and the latter swap makes sure that in other operations the sequence is unchanged.

The needed action of the random unitary(or say obj) is taken place between all qubits in qr0 and one qubit in qr1, but to actually implement this operation in Qiskit I have to expand it to action between all qubits of the two quantum registers because the QuantumCircuit object only takes QuantumRegsiter as input (not qubit) and this is computationally inefficient.

So what I want to know is: is there a way to convert between qubit object and QuantumRegister object? If so, this can be time-saving!

• The obj of function $swappedOp()$ is a random unitary, you can from qiskit.quantum_ info import random_ unitary to generate it, and it acts on all qubits of qr0 and a specific qubit(index) of qr1, but unfortunately to generate a quantum circuit I have to tensor it with an identity matrix and I want to find out a way to save it. Nov 12 '20 at 8:03

Qubit's are elements of the Quantum register. So you cannot really convert a Qubit to a register, but, as you did above, you can simply use a a list of the Qubit, [Qubit].

However you don't have to use the Qubit or QuantumRegister types at all. You can instead work with indices, which is often much more convenient. E.g. you code above could be written as

def swappedOp(obj, qr0, qr1, index):
m, n = len(qr0), len(qr1)  # store the number of qubits
circ = QuantumCircuit(m + n)
circ.swap(m - 1, m + index)
circ.append(obj, circ.qubits)  # circ.qubits are just all qubits in the circuit
circ.swap(m - 1, m + index)
return Operator(circ)


I'm not quite sure what obj in your code should act on, so I just assumed it acts on all qubits as you did above. But you can of course also change that line.

I hope this gives you the tools you need!

• What I am looking for is to use a qubit to generate a current quantum register so as to improve execution efficiency. I used to work on it once(at that time I want to convert between a classical register and a classical bit), this should not be fundamentally impossible since a bit can also be operated as a register with only one bit, what is limiting here is the restriction of qiskit code(or the lack of my familiarity of qiskit code). Thank you for answering. Nov 12 '20 at 8:07

Today I suddenly noticed that my question is stupid, at least for the code I presented. I have rewritten the code and posted it below:

from qiskit import QuantumCircuit,QuantumRegister
from qiskit.quantum_info.operators import Operator
def swappedOp(obj,qr0):
qr1=QuantumRegister(1)
circ=QuantumCircuit(qr0,qr1)
circ.swap(qr0[qr0.__len__()-1],qr1)
IN=[qr0[i] for i in range(qr0.__len__())]+[qr1[0]]
circ.append(obj,IN)
circ.swap(qr0[qr0.__len__()-1],qr1)
return Operator(circ)

qr0=QuantumRegister(2)
qr1=QuantumRegister(2)
circ=QuantumCircuit(qr0,qr1)
from qiskit.quantum_info import random_unitary
numQubits=3
obj=random_unitary(2**numQubits)
newObj=swappedOp(obj,qr0)
circ.append(newObj,[qr0[i] for i in range(2)]+[qr1[0]])


In this case, since I returned an operator, I can use a one-qubit quantum register together with the size-varying quantum register qr0 to generate an Operator object and this technique can be also useful if I returned circ.to_instruction() or circ.to_gate().

So at least in my case, the function to convert a qubit to a single-qubit quantum register is not meaningful.