# Why Circuit error when I use append(U)

I am new of this field.

I want to make custom Unitary transform. I make a test matrix with Identity matrix.

If I use UnitaryGate and append it to main circuit, it would not work.

Here is my code.

I know that main_circuit.unitary(matrix_U) works well. But why main_circuit.append(U) does not work?

from qiskit import*
from qiskit.extensions import UnitaryGate

matrix_U=[[1,0],[0,1]]
U=UnitaryGate(matrix_U)
main_circuit=QuantumCircuit(2,2)
main_circuit.append(U)
%matplotlib inline
main_circuit.draw(output='mpl')


• Welcome to quantum computing StackExchange! Commented Dec 15, 2022 at 6:09

In addition to the quantum instruction, append() method expects the qubits to attach the instruction to.

So, in your case you should call it as follows:

main_circuit.append(U, [0])


Here, I applied the unitary to the first qubit, you can specify another qubit if you want. And if your unitary is a multi-qubit unitary, the list should contain $$\log_2(N)$$ qubits where $$N$$ is the matrix dimension.

• Thanks, Now main_circuit.append(U,[0]) is working.
– 윤성원
Commented Dec 16, 2022 at 3:22

For this small case, you dont need to use the UnitaryGate class (which is used with the append method as Egretta.Thula described: append(GateClass, [qubits])). It is possible to give the matrix directly to QuantumCircuit.unitary and the amount of arguments are $$log_2(\texttt{len(main_circuit)}$$):

from qiskit import QuantumCircuit

matrix_U=[[1,0],[0,1]]
main_circuit=QuantumCircuit(1)
main_circuit.unitary(matrix_U, 0)
main_circuit.draw(output='mpl')


• That is a good alternative. Thanks
– 윤성원
Commented Dec 16, 2022 at 3:23