# How to assemble two quantum circuits whose qubit mapping states are different each other?

I uploaded similar question before, but the answer was not I want. I think I should show you in more detail about my question.

I use SABRE algorithm which can do qubit mapping for executing quantum circuit on the real quantum hardware. But I really wonder how I can assemble two quantum circuits that has each own qubit mapping state.

For example... These two quantum circuits have own their qubit mapping states respectively. I use qiskit to make programming code on linux.

Above one is the first quantum circuit.

Above one is the second quantum circuit.

As you can see they have own qubit mapping state. (First : 3 -> 6, but the second : 3 -> 7) How can I assemble them without any problem? Is there any way to do it?

I already did qiskit's operation(compose, combine, append) but they didn't work. Also '+' operation didn't work.

• Plus clarification...

This is the code how I assemble two quantum circuit

circuit = circuits[0]
for cir in circuits[1:]:  # start from index 1
circuit += cir


'circuits' is a list of quantum circuit. So, I use for loop to add circuits one by one. I also used qiskit's operations like 'append', 'combine', 'compose', 'extend', but all of them didn't working. They just couldn't add more circuits. In case of '+', it didn't consider about qubit mapping state of each quantum circuit and just add circuits directly... So it has problem too. You can see the results on my other question.

I really wonder how I can add quantum circuits ( assemble quantum circuits ) considering each qubit mapping state of them.

• why not append them before transpiling? Is there a problem with doing that? Jul 5 at 2:48
• related: quantumcomputing.stackexchange.com/q/18225/55. Also, please note you can, and usually should, edit your posts to add clarifications when needed
– glS
Jul 5 at 8:22
• AS previously mentionned , "append" should work. Can you post your code here ? and detail what you're expecting. Jul 5 at 12:37
– 김동민
Jul 6 at 7:46

An example using .append(). If that does not answer to your need, it might be a start for you to explain what's wrong ....

subcirc1 = QuantumCircuit(10)
subcirc1.h(3)
subcirc1.x(5)
subcirc1.z(7)
subcirc1.barrier()

subcirc2 = QuantumCircuit(10)
subcirc2.h(1)
subcirc2.x(3)
subcirc2.z(6)
subcirc2.barrier()
subcirc2.draw(idle_wires=False)

circ = QuantumCircuit(10)
circ.h(2)
circ.x(5)
circ.z(9)
circ.barrier()

circuit = (subcirc1, subcirc2)
for c in circuit:
circ.append(c, [0,1,2,3,4,5,6,7,8,9])
circ.decompose().draw(idle_wires=False)


This gives you the circuit here below

Based on your new additional inputs, it seems like you want to transpile the individual circuit before appending them together to potentially reduce the extra work needed for the transpiler since it won't have to transpile a large circuit.

The problem that you run into then is that when you tried to transpile each individual circuit, their new transpiled circuit are mapped to different qubits, so when append them together, the qubits get mismatched, and hence changing the original circuit you had in mind.

If that is the case, then you can specify the initial layout in the transpilation process. This will make sure the qubits on each circuit will be mapped to the same target qubits.

For example:

Circuit 1:

circuit1 = QuantumCircuit(4)
for i in range(4):
circuit1.x(i)

circuit1_transpiled = transpile(circuit1, provider.get_backend('ibmq_16_melbourne') ,
initial_layout = [4,5,9,10] ,
routing_method = 'sabre')


here you still use the Sabre routing method, and the transpiled circuit is:

  ancilla_0 -> 0 ─────

ancilla_1 -> 1 ─────

ancilla_2 -> 2 ─────

ancilla_3 -> 3 ─────
┌───┐
q_0 -> 4 ┤ X ├
├───┤
q_1 -> 5 ┤ X ├
└───┘
ancilla_4 -> 6 ─────

ancilla_5 -> 7 ─────

ancilla_6 -> 8 ─────
┌───┐
q_2 -> 9 ┤ X ├
├───┤
q_3 -> 10 ┤ X ├
└───┘
ancilla_7 -> 11 ─────

ancilla_8 -> 12 ─────

ancilla_9 -> 13 ─────

ancilla_10 -> 14 ─────



circuit 2:

circuit2 = QuantumCircuit(4)
for i in range(4):
circuit2.h(i)

circuit2_transpiled = transpile(circuit2, provider.get_backend('ibmq_16_melbourne') ,
initial_layout = [4,5,9,10] ,
routing_method = 'sabre')



which now also mapped to the same target qubits [4,5,9,10] as you can see:

  ancilla_0 -> 0 ────────────────────────────

ancilla_1 -> 1 ────────────────────────────

ancilla_2 -> 2 ────────────────────────────

ancilla_3 -> 3 ────────────────────────────
┌─────────┐┌────┐┌─────────┐
q_0 -> 4 ┤ RZ(π/2) ├┤ √X ├┤ RZ(π/2) ├
├─────────┤├────┤├─────────┤
q_1 -> 5 ┤ RZ(π/2) ├┤ √X ├┤ RZ(π/2) ├
└─────────┘└────┘└─────────┘
ancilla_4 -> 6 ────────────────────────────

ancilla_5 -> 7 ────────────────────────────

ancilla_6 -> 8 ────────────────────────────
┌─────────┐┌────┐┌─────────┐
q_2 -> 9 ┤ RZ(π/2) ├┤ √X ├┤ RZ(π/2) ├
├─────────┤├────┤├─────────┤
q_3 -> 10 ┤ RZ(π/2) ├┤ √X ├┤ RZ(π/2) ├
└─────────┘└────┘└─────────┘
ancilla_7 -> 11 ────────────────────────────

ancilla_8 -> 12 ────────────────────────────

ancilla_9 -> 13 ────────────────────────────

ancilla_10 -> 14 ────────────────────────────


Now, we can append them together the way you did in your post:

total_transpiled_circuit = circuit1_transpiled + circuit2_transpiled


which is

 q_0: ─────────────────────────────────

q_1: ─────────────────────────────────

q_2: ─────────────────────────────────

q_3: ─────────────────────────────────
┌───┐┌─────────┐┌────┐┌─────────┐
q_4: ┤ X ├┤ RZ(π/2) ├┤ √X ├┤ RZ(π/2) ├
├───┤├─────────┤├────┤├─────────┤
q_5: ┤ X ├┤ RZ(π/2) ├┤ √X ├┤ RZ(π/2) ├
└───┘└─────────┘└────┘└─────────┘
q_6: ─────────────────────────────────

q_7: ─────────────────────────────────

q_8: ─────────────────────────────────
┌───┐┌─────────┐┌────┐┌─────────┐
q_9: ┤ X ├┤ RZ(π/2) ├┤ √X ├┤ RZ(π/2) ├
├───┤├─────────┤├────┤├─────────┤
q_10: ┤ X ├┤ RZ(π/2) ├┤ √X ├┤ RZ(π/2) ├
└───┘└─────────┘└────┘└─────────┘
q_11: ─────────────────────────────────

q_12: ─────────────────────────────────

q_13: ─────────────────────────────────

q_14: ─────────────────────────────────



Thus, by specifying the initial layout you will avoid the problem of having mismatch target qubits during transpilation process.

• But when I set the initial layout and use sabre then the output of qubit mapping state will be changed. Isn't it?
– 김동민
Jul 7 at 5:09
• no. it won't. If you set the initial layout then the target qubits will be fixed. However, it will eliminate the feature of having the algorithm choosing the best qubits to be used... Jul 7 at 12:25
• Thank you for reply. I wonder one question. As I know, SABRE algorithm use random initial mapping at first and use reverse traversal technique to change initial mapping and update it to better one. So, you mean these process is ignored when I fixed initial mapping like you mentioned?
– 김동민
Jul 7 at 23:38