Generalizing a brick-wall circuit acting on qubits to acting on qudits

Recently, I have been creating a "brick layer" circuit of random unitary gates (each acting on 2 qubits) which acts on a n qubit register using cirq. A typical circuit looks like this.

(Where a top and bottom label symbolises one gate).

The code used to reproduce it is here. https://colab.research.google.com/drive/1Rs3knKOJV8CddSQLzl1HgO1L-GR7sfbK?usp=sharing

I wish to extrapolate my model to n qudits as opposed to qubits. I have been successful in applying this to qutrits. The code for this is below.

However, when I try to do this for some general qudit register. I always land up with a type error.

See the code below (which is very similar to the others)

Does anybody know how to fix this error? Help would be greatly appreciated :) I reckoned as it was easy to get the code working for qutrits it shouldn't be so hard to get it working for any general qudit register.

Cheers, Luke

Try these changes to your code:

class QuditPlusGate(cirq.Gate):
def __init__(self, dimension):
self._p = dimension
def _qid_shape_(self):
return (self._p, self._p)
...
# the rest of your code

def quditopsmoment0(n,p):
qudits = qudit_register(n,p)
u_gate = QuditPlusGate(p)
listofqops0 = []
i = 0
while i < n/2:
listofqops0.append(u_gate.on(qudits[2*i],qudits[2*i + 1]))
i = i + 1
else:
return listofqops0

quditopsmoment0(2,2)


Passing the dimension as an argument when initializing the QuditPlusGate class allows eliminating the extra argument you had for the _qid_shape_ method to match the method it was overriding. That seemed to fix it for me.

• This is absolutely perfect! Thank you so much for the help :) Mar 9, 2023 at 13:18
• @LukeMichie Awesome, if question is resolved could you mark answer as accepted? Thanks! Mar 9, 2023 at 17:38
• No problem, apologies! I am very new to StackExchange :) Mar 10, 2023 at 21:18