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!