Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'list' object has no attribute 'float' #17

Open
sleepingcat4 opened this issue Aug 18, 2023 · 7 comments
Open

AttributeError: 'list' object has no attribute 'float' #17

sleepingcat4 opened this issue Aug 18, 2023 · 7 comments

Comments

@sleepingcat4
Copy link

I was running the c2q_transfer_learning_ants_bees.ipynb notebook and received an error in the qnet function about an attribute error. I am assuming it is happenning as float point is being applied on a list rather than tensors.

Code that is causing the error:

@qml.qnode(dev, interface='torch')
def q_net(q_in, q_weights_flat):
        
        # Reshape weights
        q_weights = q_weights_flat.reshape(max_layers, n_qubits)
        
        # Start from state |+> , unbiased w.r.t. |0> and |1>
        H_layer(n_qubits)   
        
        # Embed features in the quantum node
        RY_layer(q_in)      
       
        # Sequence of trainable variational layers
        for k in range(q_depth):
            entangling_layer(n_qubits)
            RY_layer(q_weights[k + 1])

        # Expectation values in the Z basis
        return [qml.expval(qml.PauliZ(j)) for j in range(n_qubits)]

class Quantumnet(nn.Module):
        def __init__(self):
            super().__init__()
            self.pre_net = nn.Linear(512, n_qubits)
            self.q_params = nn.Parameter(q_delta * torch.randn(max_layers * n_qubits))
            self.post_net = nn.Linear(n_qubits, 2)

        def forward(self, input_features):
            pre_out = self.pre_net(input_features) 
            q_in = torch.tanh(pre_out) * np.pi / 2.0   
            
            # Apply the quantum circuit to each element of the batch and append to q_out
            q_out = torch.Tensor(0, n_qubits)
            q_out = q_out.to(device)
            for elem in q_in:
                q_out_elem = q_net(elem,self.q_params).float().unsqueeze(0)
                q_out = torch.cat((q_out, q_out_elem))
            return self.post_net(q_out)

Error Message:

Training started:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-39-efc42279baeb> in <cell line: 1>()
----> 1 model_hybrid = train_model(model_hybrid, criterion, optimizer_hybrid,exp_lr_scheduler, num_epochs=num_epochs)

5 frames
<ipython-input-33-2b46b36c83e0> in forward(self, input_features)
     14             q_out = q_out.to(device)
     15             for elem in q_in:
---> 16                 q_out_elem = q_net(elem,self.q_params).float().unsqueeze(0)
     17                 q_out = torch.cat((q_out, q_out_elem))
     18             return self.post_net(q_out)

AttributeError: 'list' object has no attribute 'float'

Can someone help me fixing this error?

@isaacdevlugt
Copy link

Hey @sleepingcat4, could you tell us what version of PennyLane you're running (you can just print qml.about())? This repo runs on a very old version of PennyLane (0.7.0 — we are currently on 0.31.1).

@sleepingcat4
Copy link
Author

I am using PennyLane version: 0.31.1

@isaacdevlugt
Copy link

Yep that explains it! With v0.30, we introduced new return types from QNodes — I think the error you're getting is most likely due to that particular change in PennyLane, but no guarantees because 0.7.0 is quite old.

We have a nice documentation page that shows how the new return types impact legacy code:

https://docs.pennylane.ai/en/stable/introduction/returns.html

Let me know if that helps!

@sleepingcat4
Copy link
Author

thanks for reference, it worked and my issue is resolved!

@sleepingcat4
Copy link
Author

I though I had fixed it but unfortunately I am geetting the error: AttributeError: 'list' object has no attribute 'float' again. It turns out while I was running it on colab, though I had restarted my runtime, it used version 0.7.0 to execute the training process.

I had earlier installed version v0.7.0 to test but installed the latest immediately afterwards as v0.7.0 didn't have device plug-in pre-packed. Can you please help me in resolving this error?

@isaacdevlugt

@sleepingcat4 sleepingcat4 reopened this Aug 19, 2023
@isaacdevlugt
Copy link

Hey @sleepingcat4, I'm having some issues getting this notebook to run on my machine due to how old it is 😅. I'll need to investigate this further. Sit tight and I or one of my colleagues will respond this week.

@KetpuntoG
Copy link

Hi @sleepingcat4 😄

The problem here is in q_net when returning this list:
return [qml.expval(qml.PauliZ(j)) for j in range(n_qubits)]

In this case the Qnode is returning a list instead of a Tensor. To solve this, I would put:

q_out_elem = torch.Tensor(q_net(elem,self.q_params)).float().unsqueeze(0)

within the forward method of the Quantumnet class.

From my point of view this is not expected behavior so I will let the team know so that they can keep it in mind and change it if they think it is appropriate 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants