pennylane.wires.WireError: Cannot run circuit(s) on default.qubit as they contain wires not found on the device:

110 Views Asked by At

I am trying to use this code to caculate for one tensor with dimension 4D (e.x. (32,32,128,2)) However, It alway appears error "pennylane.wires.WireError: Cannot run circuit(s) on default.qubit as they contain wires not found on the device and next to is a number series: {e.g, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47......} Someone can help me or give me some suggestion to fix that. Thanks! Source code

class Quantumnet(nn.Module):
    def __init__(self, feat_dim):
        super().__init__()
        self.pre_net = nn.Linear(feat_dim, n_qubits)
        self.bn = nn.BatchNorm2d(n_qubits)
        self.vqc = qml.qnn.TorchLayer(qnode, {"weights": (q_depth, n_qubits, 3)})
        self.post_net = nn.Linear(n_qubits, 2)  # Update in_features to n_qubits
        self.ce = nn.CrossEntropyLoss()
        self.initialize_params()

    def initialize_params(self):
        for m in self.modules():
            if isinstance(m, nn.Linear):
                init.xavier_normal_(m.weight.data)
                if m.bias is not None:
                    m.bias.data.fill_(0)
            elif isinstance(m, nn.BatchNorm1d):
                m.running_mean.data.fill_(0)
                m.running_var.data.fill_(1)
                
    def forward(self, input_features, labels=None):
        pre_out = self.pre_net(input_features)
        pre_out = self.bn(pre_out)
        q_in = torch.tanh(pre_out) * np.pi / 2.0
        q_out = self.vqc(q_in)
        q_out = self.post_net(q_out)
        loss = self.ce(q_out, labels)
        return loss, (q_out[:, 1] - q_out[:, 0]).squeeze(0)

feat_dim = 2  # set feat_dim to match the last dimension of input_features
n_qubits = 32
model = Quantumnet(feat_dim)

# Create a PyTorch tensor with the shape (32, 32, 128, 2)
input_features = torch.randn(32, 32, 128, feat_dim)

# Assuming you have appropriate labels for your task
labels = torch.randint(2, (batch_size,), dtype=torch.long)

# Call the model's forward method to get the output
output = model(input_features, labels=labels)

# Access the loss and tensor of interest
loss, quantum_output = output

# Print the shape of the quantum output tensor
print('Quantum Output Tensor Shape:', quantum_output.shape)```

Change forward() function, but it still not working.
0

There are 0 best solutions below