0
$\begingroup$

Can someone please help me making these gates on CIRQ programming:

  1. Decrement Gate (-1)
  2. Splitter Gate (If possible)

I was having trouble implementing these and was not sure if it's possible either.

Thanks in advance.

$\endgroup$

1 Answer 1

1
$\begingroup$

I'm not sure what a splitter operation is.

There is a class cirq.ArithmeticOperation which is a helper class for implementing arithmetic. You can implement an increment gate with it like this:

import cirq

class Increment(cirq.ArithmeticOperation):
    def __init__(self, target_register):
        self.target_register = target_register

    def registers(self):
        return [self.target_register]

    def with_registers(self, *new_registers):
        return Increment(*new_registers)

    def apply(self, target_value):
        return [target_value + 1]

The helper class will ensure that several useful methods are implemented, such as a reasonably efficient simulation method. Note that the new register value returned in apply automatically wraps modulo $2^n$ where $n$ is the size of the register. Also it is your responsibility to make sure the arithmetic operation is actually reversible.

Then you can use it like this:

register = cirq.LineQubit.range(2)
circuit = cirq.Circuit(Increment(register))

print(circuit)
# 0: ───<__main__.Increment object at 0x000002594B1F03C8>───
#       │
# 1: ───#2──────────────────────────────────────────────────


print(cirq.unitary(Increment(register)))
# [[0.+0.j 0.+0.j 0.+0.j 1.+0.j]
#  [1.+0.j 0.+0.j 0.+0.j 0.+0.j]
#  [0.+0.j 1.+0.j 0.+0.j 0.+0.j]
#  [0.+0.j 0.+0.j 1.+0.j 0.+0.j]]
$\endgroup$

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.