# What does circuit-xxx accomplish after circuit gets decomposed by qiskit?

as mentioned in the title, i have a problem with the qiskit decompose function.

AND=Operator([
[1,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0],
[0,0,0,1,0,0,0,0],
[0,0,0,0,1,0,0,0],
[0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,1,0],
])


In my code i defined an AND gate like this.

An AND Operator is actually just a CCX Gate, but i defined the matrix and then applied the unitary function with the Operator and the qubit list as parameters.

circ.unitary(AND,[0,1,2],label="AND")


After transpiling on the qasm_simulator, i get the result added in the picture below

In this example i tried to calculate q_2 AND q_1. After Measuring the result is measured at q_0. It works just fine. My question is what are these circuit mean in the picture, i found nothing about circuit-140 for example.

What i think should be expected is the decomposed toffoli gate or ccx gate.

• How did you "transpiled" the circuit? Commented Nov 30, 2022 at 9:08
• @Egretta.Thula i actually used the decompose() function, first because the transpile() function gave me just my own defined gate. Turns our that i used that function incorrectly. The decompose() function still returns the circuit above, but with transpile() and a set of gates the problem is solved Commented Nov 30, 2022 at 12:29
• To answer your immediate question. decompose() can be called multiple times if you want to get deeper into the circuit. You can also call gate.decompose(reps=5) for example, to call decompose five times. Two or three times is usually sufficient. Commented Nov 30, 2022 at 19:37

## 2 Answers

The result of the transpilation you are looking at is the consequence of unitary synthesis, which runs as part of the transpilation process:

from qiskit.transpiler.passes import UnitarySynthesis
UnitarySynthesis()(circ).draw("mpl")

<similar output than your>


There are many algorithm to convert a unitary matrix into gates. However, in general, they do not guarantee that conversion is optimal in terms of the target gates. They preserve the result, as you notice, but the "meaning" is lost. The circuit-XXX gate have no specific meaning, they are two-qubits gates that the synthesis algorithm calculated. If you want to decompose that even further as rotations, run transpile(circ, basis_gates=["cx", "u"]).

In general, it is hard to "recover semantics" (the fact that matrix was describing an AND) once you are lower in the abstraction (a matrix, in this case). If you want to preserve that, I recommend you to use less abstract constructors. In this case, BooleanExpression:

from qiskit.circuit.classicalfunction import BooleanExpression

AND = BooleanExpression('x & y', name='AND')
circ = QuantumCircuit(3)
circ.append(AND, [0,1,2])
transpile(circ, basis_gates=["ccx"]).draw('mpl')


• Thank you that helped a lot. I'm gonna have a deeper look at the booleanExpressions and the Unitary Synthesis Commented Nov 30, 2022 at 12:25

I'd like to add a few notes to @luciano's great answer. First of all, since Qiskit uses little endian bit ordering, the matrix mentioned in the question will not create a CCX gate. You should use:

$$\begin{split}CCX_{0, 1, 2} = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \end{pmatrix}\end{split}$$

You can also use reverse_bits() method.

Second, the reason your circuit contains circuit-xxx sub-circuits is that decompose() method by default decomposes one level only (shallow decompose). For further decomposition you can call decompose() multiple times:

circuit.decompose().decompose().draw('mpl')


or use reps parameter to specify the number of times the circuit should be decomposed:

circuit.decompose(reps = 2).draw('mpl')