How do I create a parameterized equation with the same coefficient for certain terms?

21 Views Asked by At

I'm working on a project that involve the pennylane package for quantum machine learning. However, knowledge of the actual package/quantum machine learning (QML) isn't necessary for the resolution of my issue, and I'm sure that this is just a trivial Python fix which I can't seem to find an answer to. If you are aware of pennylane/QML, the topic question in the heading is better posed as: "How do I create a parameterized Hamiltonian with the same coefficient for certain terms?"

But anyways, onto my issue. I'm trying to create an equation that has coefficients multiplying different operators. I do this by the function

    def create_native_hamiltonian(self):

        self.native_fields_squeezing = [self.get_control_field() for i in range(self.n_wires-1)]
        self.H_native_operators_squeezing = [qml.PauliZ(i) @ qml.PauliZ(i+1) for i in range(self.n_wires-1)] # Assuming nearest neighbor interactions

        self.native_fields_identity = [self.get_control_field() for i in range(self.n_wires)]
        self.H_native_operators_identity = [qml.Identity(i) for i in range(self.n_wires)]

        self.total_native_fields = self.native_fields_squeezing + self.native_fields_identity
        self.H_total_native_operators = self.H_native_operators_squeezing + self.H_native_operators_identity

        self.H_native = qml.dot(self.total_native_fields, self.H_total_native_operators)

    def get_control_field(self): # Maybe make some to force the physical constraints?

        def control_field(p,t):
            # p is the trainable parameter
            # t is the time
            return p

        return control_field

When I call this function for self.n_wires = 5 I get an equation that has the form

  (control_field(params_0, t)*(PauliZ(wires=[0]) @ PauliZ(wires=[1])))
+ (control_field(params_1, t)*(PauliZ(wires=[1]) @ PauliZ(wires=[2])))
+ (control_field(params_2, t)*(PauliZ(wires=[2]) @ PauliZ(wires=[3])))
+ (control_field(params_3, t)*(PauliZ(wires=[3]) @ PauliZ(wires=[4])))
+ (control_field(params_4, t)*(Identity(wires=[0])))
+ (control_field(params_5, t)*(Identity(wires=[1])))
+ (control_field(params_6, t)*(Identity(wires=[2])))
+ (control_field(params_7, t)*(Identity(wires=[3])))
+ (control_field(params_8, t)*(Identity(wires=[4])))

Thus, when I create this equation (which is just a sum of coefficients and operators) I must specify 9 different parameters for each term. However, I just want to specify two parameters and have my equation be in the form

  (control_field(params_0, t)*(PauliZ(wires=[0]) @ PauliZ(wires=[1])))
+ (control_field(params_0, t)*(PauliZ(wires=[1]) @ PauliZ(wires=[2])))
+ (control_field(params_0, t)*(PauliZ(wires=[2]) @ PauliZ(wires=[3])))
+ (control_field(params_0, t)*(PauliZ(wires=[3]) @ PauliZ(wires=[4])))
+ (control_field(params_1, t)*(Identity(wires=[0])))
+ (control_field(params_1, t)*(Identity(wires=[1])))
+ (control_field(params_1, t)*(Identity(wires=[2])))
+ (control_field(params_1, t)*(Identity(wires=[3])))
+ (control_field(params_1, t)*(Identity(wires=[4])))

where each control_field(params_0, t) is the same object and similarly for control_field(params_1, t).

For more information about parameterized Hamiltonians in the context of Pennylane click here.

Thank you for all the help!

0

There are 0 best solutions below