0

I am very new to python. I was studing some code from qiskit - https://qiskit.org/textbook/ch-machine-learning/machine-learning-qiskit-pytorch.html. I have a lot of doubt in the below code. After doing some study of neural network, I can intrepret that below code is for back progpogation. But above the function backward, the @staticmethod is written. I did some study and came to know that these are decorators. They run before the backward function is executed. Not sure what exactly this function do. I studied the neural network code for classical computer. They donot add this. I would be really if someone can explain me its use.

@staticmethod
    def backward(ctx, grad_output):
        """ Backward pass computation """
        input, expectation_z = ctx.saved_tensors
        input_list = np.array(input.tolist())
        
        shift_right = input_list + np.ones(input_list.shape) * ctx.shift
        shift_left = input_list - np.ones(input_list.shape) * ctx.shift
        
        gradients = []
        for i in range(len(input_list)):
            expectation_right = ctx.quantum_circuit.run(shift_right[i])
            expectation_left  = ctx.quantum_circuit.run(shift_left[i])
            
            gradient = torch.tensor([expectation_right]) - torch.tensor([expectation_left])
            gradients.append(gradient)
        gradients = np.array([gradients]).T
        return torch.tensor([gradients]).float() * grad_output.float(), None, None
mchaudh4
  • 65
  • 5

1 Answers1

1

The @staticmethod decorator indicates that this method is static, which means that it can be run without being attached to a particular instance of the class.

It simply means that you can consider it as a normal function, the only thing to remember is that you will call it with Class.backward(ctx, grad_output), with Class being the name of the class this method is declared in.

For instance, consider the following code:

class MyClass:
    def my_method(self, a):
        print(a)
    
    @staticmethod
    def my_second_method(a):
        print(a)

    def my_third_method(a):
        print(a)

my_instance = MyClass()
my_instance.my_method(2) # Prints 2
MyClass.my_second_method(3) # Prints 3
my_instance.my_third_method(4) # Error

When callig a method in Python, a reference to the calling object is passed as the first argument, this is the self in the first call. When a @staticmethod decorator is used, this is not the case, and the method should be called using the class name.

As a proof, the last line throws an error: my_third_method expects a single argument, but is being passed my_instance as a first_argument and 4 as a second argument.

A better explanation of this decorator can be found on Software Engineering SE and on StackOverflow directly.

Tristan Nemoz
  • 1,844
  • 1
  • 6
  • 19