Open
Description
Describe the bug
The results of the "QubitRegister::ExpectationValueX", "...Y" and "...Z" are incorrect if the state is not normalized.
To Reproduce
The error can be seen from the following code snippet:
QubitRegister<ComplexDP> test(1, "base", 0);
iqs::TinyMatrix<ComplexDP, 2, 2, 32> IDby2;
IDby2(0,1) = IDby2(1,0) = ComplexDP(0., 0.);
IDby2(0,0) = IDby2(1,1)= ComplexDP(std::sqrt(0.5), 0.);
test. Apply1QubitGate(0, IDby2); // state now has a square norm of 1/2.
test.ApplyPauliX(0); //state is |\psi> = 1/sqrt(2) |1>
assert(test.ExpectationValueZ(0) == -0.5); //value should be -1/2 due to un-normalized state
//actual value: test.ExpectationValueZ(0) == 0.
Additional context
I was able to track down the problem. In the expectation value functions, a gate is applied to change the basis to the appropriate Pauli operator, and then the value is calculated via the lines 24, 50 and 72 of "src/qureg_expectval.cpp":
BaseType expectation = 1. - 2.*GetProbability(qubit);
This expression assumes the vector is normalized. In particular 1. -> (state norm)^2 for this to be correct in general.
Metadata
Metadata
Assignees
Labels
No labels