Hadamard Test Not working properly

Hi there!

New user here, I’m a physics master student and am starting my travel into QC. For a project I needed to evaluate the overlap between two functions <v1|v2> and thought that using the “Hadamard Overlap Test” would be the easiest way to do it. However when I went to implement it I ran into quite a issue. In the form I’m showing below the code runs and calculates the correct inner product for certain states, such as:

v1 = 1/np.sqrt(2)*np.array([0,0,1,1])
v2 = 1/np.sqrt(2)*np.array([0,1,1,0])

for which I get 0.5. On the other hand, for other states it gives out the wrong result, for example for

v1 = 1/np.sqrt(2)*np.array([0,0,-1,1])
v2 = 1/np.sqrt(2)*np.array([0,1,1,0])

instead of getting -0.5 I get -0.35355339-0.35355339i. Can anyone help me understand what I’m doing wrong?

import pennylane as qml

circ = qml.device("default.qubit",wires = 6)

def inner(v1,v2):
    qml.Hadamard(wires = 0)
    qml.Hadamard(wires = 3)
    qml.adjoint(qml.S(wires = 3))
    qml.ctrl(qml.StatePrep(v2,wires = [1,2]),control = 0)
    qml.ctrl(qml.StatePrep(v2,wires = [4,5]),control = 3)
    qml.ctrl(qml.adjoint(qml.StatePrep(v1,wires = [1,2])),control = 0)
    qml.ctrl(qml.adjoint(qml.StatePrep(v1,wires = [4,5])),control = 3)
    qml.Hadamard(wires = 0)
    qml.Hadamard(wires = 3)
    return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(3))

Here is the version I’m running, in case it’s relevant

Name: PennyLane
Version: 0.33.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
License: Apache License 2.0
Location: /Users/lorenzo/anaconda3/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning, PennyLane-qiskit

Platform info:           macOS-14.4.1-arm64-arm-64bit
Python version:          3.11.7
Numpy version:           1.26.4
Scipy version:           1.11.4
Installed devices:
- default.gaussian (PennyLane-0.33.0)
- default.mixed (PennyLane-0.33.0)
- default.qubit (PennyLane-0.33.0)
- default.qubit.autograd (PennyLane-0.33.0)
- default.qubit.jax (PennyLane-0.33.0)
- default.qubit.legacy (PennyLane-0.33.0)
- default.qubit.tf (PennyLane-0.33.0)
- default.qubit.torch (PennyLane-0.33.0)
- default.qutrit (PennyLane-0.33.0)
- null.qubit (PennyLane-0.33.0)
- lightning.qubit (PennyLane-Lightning-0.33.1)
- qiskit.aer (PennyLane-qiskit-0.32.0)
- qiskit.basicaer (PennyLane-qiskit-0.32.0)
- qiskit.ibmq (PennyLane-qiskit-0.32.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.32.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.32.0)
- qiskit.remote (PennyLane-qiskit-0.32.0)

Thank you in advance!


Hi @Lorenzo, welcome to the Forum!

I’m running your code in Google Colab, using v0.35.1 of PennyLane and I am getting the expected 0.5 and -0.5 for the tests.

I’m guessing something must have been wrong in version 0.33 but it’s now fixed.

You can upgrade your version of PennyLane by running pip install pennylane --upgrade

I hope this helps!

1 Like

Thank you so much! This worked, I wander what the bug was…

I’m glad it worked @Lorenzo!

We will have a new release coming out in 1 week so you can also try that one out and let me know if you have any issues.

Enjoy using PennyLane!