Wire Mismatch Error When Using lightning.qubit for QFIM

Hello PennyLane!

I want to use the lightning.qubit device in PennyLane to run a parametric quantum circuit with two qubits and compute the quantum Fisher information. However, when I try to do this, I get a WireError stating that my circuit contains a wire (wire-2) that is not found on the device. Strangely, the same code runs without issues on default.qubit.

Thank you in advance for the advices.

import numpy as np
import pennylane as qml
from pennylane import numpy as pnp
pnp.random.seed(42)

# System parameters
num_nodes = 2
num_qubits_per_node = 1
total_qubits = num_nodes * num_qubits_per_node

#dev = qml.device('default.qubit', wires=total_qubits)
dev = qml.device("lightning.qubit", wires=range(total_qubits)) 

@qml.qnode(dev)
def circuit(param):
    layers = len(param[:, 0, 0])
    n_wires = len(param[0, :, 0])
    n_params_rot = len(param[0, 0, :])
    # Applying repeated trainable layers consisting parametized rotations and entangling gates
    for i in range(layers):
        range_value = (i % (n_wires - 1)) + 1
        qml.StronglyEntanglingLayers(param[i, :, :].reshape(1, n_wires, n_params_rot), wires=range(total_qubits), ranges=None)
    return qml.state()

wires=total_qubits
layers=2
rng = pnp.random.default_rng()
params_shape = qml.StronglyEntanglingLayers.shape(n_layers=layers,n_wires=wires)
params = rng.random(size=params_shape)
params = pnp.array(params, requires_grad=True)

qml.gradients.quantum_fisher(circuit)(params)

This is the error message I got:

---------------------------------------------------------------------------
WireError                                 Traceback (most recent call last)
<ipython-input-52-0f12bc5f17c5> in <cell line: 0>()
----> 1 qml.gradients.quantum_fisher(circuit)(params)

11 frames
/usr/local/lib/python3.11/dist-packages/pennylane/devices/preprocess.py in validate_device_wires(tape, wires, name)
    146 
    147         if extra_wires := set(tape.wires) - set(wires):
--> 148             raise WireError(
    149                 f"Cannot run circuit(s) on {name} as they contain wires "
    150                 f"not found on the device: {extra_wires}"

WireError: Cannot run circuit(s) on lightning.qubit as they contain wires not found on the device: {2}

The qml version I used:

Name: PennyLane
Version: 0.40.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /usr/local/lib/python3.11/dist-packages
Requires: appdirs, autograd, autoray, cachetools, diastatic-malt, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, tomlkit, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-6.1.85+-x86_64-with-glibc2.35
Python version:          3.11.11
Numpy version:           1.26.4
Scipy version:           1.13.1
Installed devices:
- default.clifford (PennyLane-0.40.0)
- default.gaussian (PennyLane-0.40.0)
- default.mixed (PennyLane-0.40.0)
- default.qubit (PennyLane-0.40.0)
- default.qutrit (PennyLane-0.40.0)
- default.qutrit.mixed (PennyLane-0.40.0)
- default.tensor (PennyLane-0.40.0)
- null.qubit (PennyLane-0.40.0)
- reference.qubit (PennyLane-0.40.0)
- lightning.qubit (PennyLane_Lightning-0.40.0)

Hi @a.rozgonyi96 ,

The key here is that calculating the QFIM requires the use of a metric tensor, which requires an extra qubit. So instead of defining your device as

dev = qml.device("lightning.qubit", wires=range(total_qubits))

you should define it as

dev = qml.device("lightning.qubit", wires=total_qubits+1)

This should solve the issue.

More context:
The note in the documentation for QFIM mentions that qml.metric_tensor is used under the hood, unless you use default.qubit with shots=None. This means that if you’re using lightning.qubit, then qml.metric_tensor is used. Now if you look at the docs for qml.metric_tensor you’ll notice that it requires an additional (aux) wire.

I hope this helps!

1 Like

Dear @CatalinaAlbornoz

Thank you, it makes a lot of sense. Sorry I haven’t noticed the note from the documentation.
Can I have a quick question regarding how I want to get back the state vector of the original 2 qubits? One way while using ‘qml.state()’ is to trace out the third extra one, but I’m wondering if there would be a more elegant solution? Thank you in advance.

Hi @a.rozgonyi96 ,

No worries about the note in the documentation. It was quite hidden. I’ll make a note to add this more clearly in the docs for QFIM.

Unfortunately I don’t know of any elegant solution to your second question. For other types of measurements you would be able to use wires=[0,1] within your measurement, but unfortunately you cannot select a specific set of wires when you use qml.state().

Thank you for your help @CatalinaAlbornoz !

1 Like