# deliver a quantum register to/from a function in qiskit

Can I create a function in qiskit which receive a quantum register as an argument, and/or returns a quantum register? In the example below, I created a quantum register, and a quantum circuit which works on it in the main function. I deliver this register to a function called "test" which chooses a random state for this register, but when I perform a measurement in the main function, it seems like the original state was not effected by the operation of the function since the measurement gives:

counts: {'000': 1024}

Can any one explain this? Is there a correct way to do so? Thanks


#######################################################################
def test(n, QP, CP):

QC_B = QuantumCircuit(QP, CP)

# choose a random state
for i in range(n):
if (np.random.randint(2) == 1):
QC_B.x(QP[i])
else:
QC_B.iden(QP[i])
QC_B.barrier()

simulator = Aer.get_backend('qasm_simulator')
result = execute(QC_B, simulator)

return QP

#######################################################################
def main():

N = 3
Q = QuantumRegister(N)
C = ClassicalRegister(N)
QC_A = QuantumCircuit(Q, C)

# ...
# ... QC_A perform some operations on Q...
# ...

QC_A.iden(Q)
Q = test(N, Q, C)

QC_A.measure(Q, C)

simulator = Aer.get_backend('qasm_simulator')
# Execute and get counts
result = execute(QC_A, simulator).result()
counts = result.get_counts(QC_A)
print("counts: ")
print(counts)
return

$$$$

• Quantum Register is just a placeholder. When you add the X gate to your circuit QC_B in function test, it's added to your circuit QC_B and not to the register QP. You should consider returning circuit QC_B from this function and then make measurement on this circuit in your main function.
– ss09
Dec 26 '19 at 4:43
• @ss09 Thanks for the reply. You are right, but my question is more general, and this is just a simple example for it. The general question is - Can I call a function and give to it a quantum register, and the function will work on it and return the modified register to the caller which will continue to work on the modified state of the register?
– AL_P
Dec 26 '19 at 7:07
• AFAIK, we can't operate on registers outside of a circuit. You could pass circuit as a parameter to a function, manipulate the circuit inside the function and then return it to the caller which will then have an altered circuit. But we can't do same thing with the register since there's nothing called "modifying a register".
– ss09
Dec 26 '19 at 7:26
• A small clue is if we look at your test function, it only manipulates QC_B circuit and the register QP` is actually left untouched and which is expected. We manipulate a qubit inside a circuit. A QuantumRegister just provides a placeholder for said number of qubits using which we can create a QuantumCircuit.
– ss09
Dec 26 '19 at 7:32
• @ss09 I changed the "test" function and now it calls execute, to "fix" the fact that the function does not touch the qubits. The result is unsurprisingly the same. So just to be sure, I can't return a register with a certain state, and I can't give a register to a function and expect that it will modify the state of the qubits of the register and that the change will be valid in the calling function?
– AL_P
Dec 26 '19 at 8:58