Suppose I have a series of quantum circuits, each of them has 2 qubits:

qc = QuantumCircuit(2)
circ_list = [...say this is a list with 10 elements, each of them has the same structure above...]

If I also have a larger quantum circuit:

qcl = QuantumCircuit(8,8)

I want to compose each of the 10 'small circuits' onto the large circuit, I think a standard method would be:

qcom = qcl.compose(circ_list[i],[2,3]) # [2,3] is a random choice

However, if I want to simultaneously compose 10 circuits, it looks like I need to repeat the argument 10 times. I'm wondering is there a simpler way I can do that? An analogy I can think of is the reduce function:

n = [4,3,2,1]
a = reduce(lambda x,y: x*y, n)

Can I do something similar to the reduce function to compose everything at once? Thanks!!


1 Answer 1


Sure, you can do this with reduce but your list needs to also have the information about which registers you are composing onto.

So if your circ_list was a list of tuples where the first component was the circuit object and the second component was a list indicating which registers this circuit will be composed onto like [2,3]

Then you should be able to use reduce as:

reduce(lambda x,y: x.compose(y[0],y[1]),circ_list, qcl)

See the docs on reduce for more description: https://docs.python.org/3/library/functools.html#functools.reduce

  • $\begingroup$ Thanks for the answer! What's the function of circ_list and qcl here? $\endgroup$
    – ZR-
    Feb 19, 2021 at 16:08
  • 1
    $\begingroup$ circ_list is the list of things you are reducing just like in the example you gave the list n. qcl is the initial value which we start from, if we didnt specify qcl as the initial value i think the first element of the list circ_list would have been chosen instead. thats what happens in the example you gave, we dont have to specify 1 as the initial value because its ok if the list just takes the initial value as the first value in the list $\endgroup$
    – shashvat
    Feb 19, 2021 at 16:12
  • $\begingroup$ Thanks, that helps a lot! $\endgroup$
    – ZR-
    Feb 20, 2021 at 1:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.