How to calculate FLOPs, MACs for Hybrid Quantum Classical model

Hi,

I am working with a Hybrid Quantum Classical model, using Pennylane - Torch plugin.

class QuantumModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.q_layer = qml.qnn.TorchLayer(full_circuit, weight_shapes)
        self.dropout = nn.Dropout(p=0.5) 
        self.fc = nn.Linear(n_qubits, n_classes, dtype=torch.float64)

    def forward(self, x):
        q_out = self.q_layer(x)      # Shape: [batch_size, 10]
        q_out = self.dropout(q_out)  # Apply dropout
        logits = self.fc(q_out)
        return logits

This is my model framework for the hybrid part. There are classical components also.

I want to ask: Can I measure the FLOPs and MACs the same way as I would for the classical models.

For example, this is a code to measure the FLOPs and MACs for the ResNet50 - My quantum model

from calflops import calculate_flops
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
batch_size = 1
input_shape = (batch_size, 1, 1024)
flops, macs, params = calculate_flops(model=model, 
                                      input_shape=input_shape,
                                      output_as_string=True,
                                      output_precision=4)
print("Quantum model FLOPs:%s   MACs:%s   Params:%s \n" %(flops, macs, params))

from calflops import calculate_flops
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = QuantumModel()
batch_size = 1
input_shape = (batch_size, 1, 1024)
flops, macs, params = calculate_flops(model=model, 
                                      input_shape=input_shape,
                                      output_as_string=True,
                                      output_precision=4)
print("Quantum model FLOPs:%s   MACs:%s   Params:%s \n" %(flops, macs, params))

I have read that the calculate flops library is for any models, provided that the model uses PyTorch. But I am not sure if this is the correct way to calculate the metrics for a quantum model like mine?

Is there any other method to calculate the performance metrics for a quantum model?

Hi @hiimhoanglam ,

I haven’t seen anything like this specifically for quantum. Have you tried using these functions on your hybrid PennyLane models? My recommendation would be to try them out and let us know in case it works or fails. If you’re running on a simulator there’s a good chance it might work. Otherwise maybe you can use something like timeit and make the calculation yourself.

I hope this helps!

I have tried it, and the result looks a bit weird

QuantumModel(
  10.49 K = 100% Params, 10.31 KMACs = 100% MACs, 717.1 KFLOPS = 100% FLOPs
  (input_proj): Linear(10.25 K = 97.7214% Params, 10.24 KMACs = 99.321% MACs, 20.48 KFLOPS = 2.8559% FLOPs, in_features=1024, out_features=10, bias=True)
  (q_layer): <Quantum Torch Layer: func=full_circuit>
  (dropout): Dropout(0 = 0% Params, 0 MACs = 0% MACs, 0 FLOPS = 0% FLOPs, p=0.2, inplace=False)
  (fc): Linear(77 = 0.7341% Params, 70 MACs = 0.679% MACs, 140 FLOPS = 0.0195% FLOPs, in_features=10, out_features=7, bias=True)
)
---------------------------------------------------------------------------------------------------
Quantum model FLOPs:717.1 KFLOPS   MACs:10.31 KMACs   Params:10.489 K

There is a mismatch between the FLOPS and the MACs. Supposedly the FLOPS should be 2 times the MACs, since one MAC = 2 FLOP, but this is not the case here? Is there anyway to explain this, or can we just induce the final result, just by guessing (Final result will have FLOPS around 1k4M (717.1 * 2).

This is an sample result from ResNet50

ResNet50 FLOPs: 8.2111 GFLOPS   MACs: 4.0892 GMACs   Params: 25.557 M

Hi @hiimhoanglam ,

I guess when doing quantum simulation the convention of MAC = 0.5 FLOP doesn’t stand. This could also be an artifact if the qlayer is being treated as a black box, which it seems like it is.

In any case, remember that MAC and FLOP essentially measure different things and, since multiplication and addition on a quantum computer are way harder than on a classical one, then the “standards” from the classical world may not apply.

Once again, I’m not an expert on MACs and FLOPs so if you want to know for sure what’s happening then you may need to look into the classical libraries and calculations involved in calculating these numbers.

I hope this helps!