This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
Bug in the internal SpreadZ
operation (used in Exp
decomposition)
#1021
Labels
bug
Something isn't working
Describe the bug
I think that the internal
SpreadZ
operation has a bug in it:qsharp-runtime/src/Simulation/TargetDefinitions/Decompositions/Utils.qs
Lines 7 to 16 in 70c881d
TL;DR: The
CNOT
in L9 should be called AFTER the recursiveSpreadZ
calls in L12-13.This operation intends to sum up the parities from all the qubits
to
into the first qubitfrom
. However, the second half of theto
register (to[half + 1 .. Length(to) - 1]
in L12) is summed up intoto[0]
, andto[0]
does not carry information into thefrom
qubit after this stage.I think that L9, which "transfers" the information from
to[0]
intofrom
, should appear after L12, to sum up all the qubit parities correctly.Here is an image of the
SpreadZ
circuit with four qubits in total:We see that at the end of the circuit, q0 contains the parities of q0 + q1 + q2, but not q3.
If the first CNOT gate (=L9) is applied last, q0 would contain the parities from all the qubits - q0 + q1 + q2 + q3, as it should.
This image depicts a correct implementation:
The parity of q3 is transferred into q0 indirectly through the first and last CNOT gates.
Some context for this issue - I was interested in how Q# implements the
Exp
operation. I found out that:dotnet run
, for example), theExp
operation is directly simulated, and the above decomposition isn't used. That's probably why this issue was unnoticed.qsharp-runtime
):Exp
->ExpUtil
->SpreadZ
.I suspect the
SpreadZ
operation is incorrect.This issue may gain more importance in light of #999.
To Reproduce
As a toy problem, let's consider the following quantum algorithm with four qubits and one$Z\otimes Z\otimes Z\otimes Z$ :
Exp
operation withNow, let's apply the$e^{i\theta Z\otimes Z\otimes Z\otimes Z} = Exp$ on $\ket{\psi_0}$ .
Exp([PauliZ, PauliZ, PauliZ, PauliZ], theta, _)
operationExpected behavior
Since both states in$\ket{\psi_0}$ (0000 and 1001) have even parity, we expect $\theta_0 = \theta_1 = \theta$ .
Therefore,
The first qubit is always measured to be 0.
Actual behavior
The parity of the last qubit isn't summed up - the states of$\ket{\psi_0}$ gain an opposite phase: $\theta_0 = \theta$ but $\theta_1 = -\theta$ .$\theta = \pi/2$ :
Specifically, for
And the first qubit is always measured to be 1.
Indeed, when abiding to the current
SpreadZ
implementation, the qubit is measured in the 1 state.Q# code example
The following GitHub gist provides a Q# code that demonstrates the above problem:
https://gist.github.com/jond01/de5753e23542cead2786481f82d694a7
System information
The text was updated successfully, but these errors were encountered: