CQCL / lambeq

A high-level Python library for Quantum Natural Language Processing
https://docs.quantinuum.com/lambeq/
Apache License 2.0
459 stars 113 forks source link

Inner product between two quantum circuits #154

Open kinianlo opened 2 months ago

kinianlo commented 2 months ago

Problem

Computing the inner product between two circuits with post-selection gives surprising results.

Minimal working example Consider the following two states:

  1. (state 1) a Bell state with one post-selected qubit
    
    from lambeq.backend.quantum import Ket, H, CX, Id, qubit, Bra, Scalar
    from lambeq.backend.drawing import draw_equation

state1 = Ket(0) @ Ket(0) state1 >>= H @ Id(qubit) state1 >>= CX state1 >>= Bra(0) @ Id(qubit)

print(state1.eval()) # [0.70710678 0. ]

draw_equation(state1, Ket(0) @ Scalar(1/2**0.5))

![7e1600d3-139e-4232-bccd-96b268d65344](https://github.com/user-attachments/assets/d68370db-cafc-4b9e-988d-c9751ab6cdac)
2. (state 2) the plus state:
```python
state2 = Ket(0) >> H

print(state2.eval()) # [0.70710678 0.70710678]

state2.draw(figsize=(1, 1))

c51149b9-f7be-4f76-88e5-6b9a26a1ae2b

The inner product should be computed by composing state 1 and the dagger of state 2:

inner = state1 >> state2.dagger()

print(inner.eval()) # 0.5000000000000001

draw_equation(inner)

459854e7-8a9a-4981-a55a-cae5422bdad0

That evaluates to $$1/2$$, which is technically correct as state 1 is equal to a zero state with a $$1/\sqrt{2}$$ scalar factor due to the post-selection. However, one would naturally expect the inner product between a zero state and a plus state to be $$1/\sqrt{2}$$. The issue here is that state 1 is not normalised due to the post-selection.

There are circumstances where you would want to compute the inner product between normalised states even if there were post-selected qubits. One example is computing similarity between two sentences, using the inner product between their DisCoCat circuits. This behaviour means that the use of RemoveCupsRewriter would potentially alters the evaluation of an inner product diagram by removing post-selection qubits.

Ideas

It could be helpful to have a mechanism that tracks the (post-selected) Bra's from state 1. The evaluation of the diagram should be normalised by the post-selected Bra's from state 1.

In the above example, the left Bra is post-selected. The corrected evaluation should be $$1/2$$ normalised by the post-selection success fraction $$1/\sqrt{2}$$, resulting in the desired result ($$1/\sqrt{2}$$) as the inner product between a zero state and a plus state.

y-richie-y commented 2 months ago

There is usually a sqrt(2) renormalisation term when the cup is converted to a bell effect. Would this solve the issue?

kinianlo commented 2 months ago

I doubt if any pre-determined renormalisation factor would resolve the issue. See the following example with the sqrt(2) factors:

from lambeq import BobcatParser
from lambeq import AtomicType, IQPAnsatz
from lambeq import NumpyModel

parser = BobcatParser()
ansatz = IQPAnsatz({AtomicType.NOUN: 1, AtomicType.SENTENCE: 1}, n_layers=1)

sent = "Cats love dogs"
diag = parser.sentence2diagram(sent)
circ = ansatz(diag)

# inner product with itself should produce 1 if everything is properly normalized
inner = circ >> circ.dagger() 
inner.draw(figsize=(10, 5))

model = NumpyModel.from_diagrams([inner])
model.initialise_weights()
print(model.get_diagram_output([inner])) # [0.17433632]

8c4bbf8e-2f2b-4dca-915e-ebde72447d0a

I think the exact renormalisation factor needed can only be known by running the circuit in general.

dimkart commented 2 months ago

@kinianlo You make a valid point, but we need to think a little what is the best way to fix this. I'll leave this issue open and come back to you.