I am trying to familiarise myself with Cirq, and I came across something I don't know how to code.

I would like to define two sets of qubits as follows: say the number of qubits in set 1 and 2 are both 3, then I would like my qubits in set 1 to be [cirq.GridQubit(0,0), cirq.GridQubit(0,1), cirq.GridQubit(0,2)] and my qubits in set 2 to be [cirq.GridQubit(1,0), cirq.GridQubit(1,1), cirq.GridQubit(1,2)]. I would like to be able to generalise this for an arbitrary number of qubits. In other words, I would like to be able to refer to a qubit in set 1 as cirq.GridQubit(0,i), and to a qubit in set 2 as cirq.GridQubit(1,j), for 0≤i<number of qubits in set 1 and 0≤j<number of qubits in set 2.

I can create my first set of qubits using qubits_set1 = cirq.GridQubit.rect(1, number_qubits_set1), but I am not sure how to create the second set?

Many thanks!


1 Answer 1


You can do this using the top keyword argument for cirq.GridQubit.rect which controls the starting index value for the first (topmost) row of the rectangle is indexed from:

qubits_set1 = cirq.GridQubit.rect(1, num_qubits_set1, top=0)
qubits_set2 = cirq.GridQubit.rect(1, num_qubits_set2, top=1)

The left keyword provides analogous behavior for the column indexing by controlling the starting index value for the first column

  • $\begingroup$ Thanks! That's exactly what I was looking for! $\endgroup$
    – Q.Ask
    Jun 29, 2022 at 20:10

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.