The short answer to composed circuit is the following. Given circuit1
and circuit2
, you can do like this:
circuit = circuit1 + circuit2
You can also do that with transpiled circuits:
transpiled1 = transpile(circuit1, backend, routing_method='sabre')
transpiled2 = transpile(circuit2, backend, routing_method='sabre')
circuit = transpiled1 + transpiled2
Notice that the circuits to composed need to be the same size. After transpilation, that is ensured. transpile
will make the circuit as big as the backend (given that you use the same backend during transpilation). The operation +
will wire the links one-to-one.
Here is an example to compose circuit with different sizes:
circuit1 = QuantumCircuit(5)
circuit1.mcx([0, 1, 3, 4], 2)
print(circuit1)
circuit2 = QuantumCircuit(2)
circuit2.cx(0, 1)
print(circuit2)
q_0: ──■──
│
q_1: ──■──
┌─┴─┐
q_2: ┤ X ├
└─┬─┘
q_3: ──■──
│
q_4: ──■──
q_0: ──■──
┌─┴─┐
q_1: ┤ X ├
└───┘
In this case, you need to use compose(..., qubits=...)
. The parameter qubits indicates how to wire the circuits.
circuit = circuit1.compose(circuit2, qubits=[3,2])
print(circuit)
q_0: ──■───────
│
q_1: ──■───────
┌─┴─┐┌───┐
q_2: ┤ X ├┤ X ├
└─┬─┘└─┬─┘
q_3: ──■────■──
│
q_4: ──■───────