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.

enter image description here

(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.

enter image description here

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


1 Answer 1


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
    return listofqops0

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.

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

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.