# Why For loop is not appending the circuit in the first iteration causing qubit argument mismatch with the gate expectation?

Why For loop is not appending the circuit in the first iteration, it only starts executing on the second iteration, due to which the qubit argument is mismatching with the gate expectation?

grover_operator = RemoveBarriers()(grover_operator)
n=3
lg=4
for i in range(n):
Q = circuit_to_gate(grover_operator).control().power(2**((n-1)-i))
j = i+1
qp=list(np.arange(n-j,n+lg+2))
for l in range(len(qp)):
print(qp[l])
print(" ")
qpe.append(Q,qp)


The output error it shows is:

2
3
4
5
6
7
8

1
2
3
4
5
6
7
8

---------------------------------------------------------------------------
CircuitError                              Traceback (most recent call last)
<ipython-input-92-a05ec5674012> in <module>
10         print(qp[l])
11     print(" ")
---> 12     qpe.append(Q,qp)
13

~/.local/lib/python3.7/site-packages/qiskit/circuit/quantumcircuit.py in append(self, instruction, qargs, cargs)
1083
1084         instructions = InstructionSet()
-> 1085         for (qarg, carg) in instruction.broadcast_arguments(expanded_qargs, expanded_cargs):
1086             instructions.add(self._append(instruction, qarg, carg), qarg, carg)
1087         return instructions

199         if len(qargs) != self.num_qubits or cargs:
200             raise CircuitError(
--> 201                 f"The amount of qubit({len(qargs)})/clbit({len(cargs)}) arguments does"
202                 f" not match the gate expectation ({self.num_qubits})."
203             )

CircuitError: 'The amount of qubit(8)/clbit(0) arguments does not match the gate expectation (7).'
$$$$


If grover_operator requires $$N$$ qubits, then the controlled version requires $$N + 1$$ qubits.

According to your code snippet, you pass a list of qubits, qp, to it. This list is of different length for each iteration in the outer loop. Its length is 7 then 8 then 9.

You need to do something like that:

qp = list(np.arange(n, n + lg + 2))

for i in range(n):
# Here, create Q gate
qpe.append(Q, [i] + qp)
`
• Got it! Thanks a ton! Feb 4, 2022 at 17:42