I have a quantum circuit that loads the data and is acting on qubit q0,q1,q2 then another circuit that does the same on q3,q4,q5 then I have to apply an CDKMRippleCarryAdder and few other ansatz parameterized gates acting on specific qubits. How can I add this all to one big circuit and run it. I already tried add function, but it doesn't specify which qubits should it be applied. I also tried compose function, although it has the parameter of specific qubits, it adds classical registers to the circuit, which in the end are giving me trouble in applying the GroverOperator of the EstimationProblem function. Is there any other way


3 Answers 3


QuantumCircuit.compose() method will not add classical registers to the result unless you pass a circuit that already contains classical registers. So, if your do something like:

circ1 = QuantumCircuit(3)
circ1.h([0, 1, 2])

circ2 = QuantumCircuit(3)
circ2.x([0, 1, 2])

circ = QuantumCircuit(6)
circ.compose(circ1, [0, 1, 2], inplace=True)
circ.compose(circ2, [3, 4, 5], inplace=True)

circ will not have any classical registers.

If, however, say circ1 contains a classical register, then you will have to remove its classical bits before passing it to compose method as follows:

circ1 = QuantumCircuit(3, 3)
circ1.h([0, 1, 2])

from qiskit.converters import circuit_to_dag, dag_to_circuit
dag = circuit_to_dag(circ1)
circ1 = dag_to_circuit(dag)

In Qiskit, you can create a composite circuit using the compose function while specifying the qubits that the second circuit should act on. However, as you noticed, the compose function also adds classical registers which is causing issues with your Grover Operator.

If the classical registers are not necessary for your use case, one way to bypass this problem is to manually add the gates of the second circuit to the first circuit on the desired qubits.

Here's an example of how you might do it:

from qiskit import QuantumCircuit

# Assume you have two circuits: circuit1 and circuit2

circuit1 = QuantumCircuit(3)
circuit2 = QuantumCircuit(2)

# Add gates to circuit1 and circuit2 as needed
# ...

# Create a new circuit with enough qubits
circuit = QuantumCircuit(max(circuit1.num_qubits, circuit2.num_qubits))

# Add the gates of circuit1 to the new circuit
for gate in circuit1.data:
    circuit.append(gate[0], gate[1])

# Add the gates of circuit2 to the new circuit, on the desired qubits
for gate in circuit2.data:
    qubits = [q.index for q in gate[1]]
    # Shift the qubit indices by the desired amount
    qubits = [q + circuit1.num_qubits for q in qubits]
    circuit.append(gate[0], qubits)

This code will create a new circuit with the same gates as circuit1 and circuit2, but circuit2's gates will be applied to the qubits shifted by circuit1.num_qubits.

After this, you should be able to apply your CDKMRippleCarryAdder and ansatz parameterized gates on the specific qubits without having unnecessary classical registers in your circuit, hopefully resolving the issues with the Grover Operator.

Keep in mind that this code snippet may need to be adjusted depending on the specific details of your problem and your circuits.


The above answers are good enough. You could also refer to this video from Qiskit's official YouTube channel. It presents various methods for joining two circuits.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.