I am conditionally unpreparing an amplitude encoded state that I previously prepared with qml.StatePrep, by using its adjoint qml.adjoint(qml.StatePrep)). Since I start with the [1,0,0,0] state for a 2-qubit system for example, I would expect after preparing the state and then subsquently applying the adjoint of this preparation that I would be returned to the same ground state, but instead I am left with a phase offset.
- 0 negative amplitudes to prepare, gives no offset
- 1 negative amplitude gives an offset of pi/4
- 2 negative amplitude gives an offset of pi/2
- 3 negative amplitude gives an offset of 3pi/4
- all negative amplitude gives an offset of pi
For three qubits, one negative amplitude with the rest positive gives a phase offset of pi/8.
I need to return the state to [1,0,0,0] (and in my personal application, this reversal is controlled by another entangled register, so I do not think a reset can work for me). Is there a built in way to account for this phase factor? If not I can apply an RZ gate to the first qubit in this register to account for it, but is there some built-in functionality to account for this?
import pennylane as qml
state = np.array([-0.5, 0.2, 0.3, 0.9, 0.5, 0.2, 0.3, 0.9], requires_grad=True)
state = state / np.linalg.norm(state)
test = qml.device('default.qubit', wires=3)
@qml.qnode(test)
def test_circ(state):
qml.StatePrep(state=state,wires=[0,1,2])
qml.adjoint(qml.StatePrep(state=state,wires=[0,1,2]))
# optionally apply some RZ shift to correct the phase offset
# in this specific case, it is off by pi/8
# qml.RZ(phi=np.pi/4,wires=0)
return qml.state()
print(test_circ(state))
The state I get instead of [1,0,0,0,0,0,0,0] is:
[ 9.23879533e-01+3.82683432e-01j -9.68859404e-18+5.12040449e-17j
1.96261557e-16+1.27570012e-16j 1.70680150e-17+2.90657821e-17j
-1.11022302e-16+0.00000000e+00j 9.68859404e-18-9.68859404e-18j
3.92523115e-17-9.81307787e-18j -1.70680150e-17+1.70680150e-17j]
Also, if I had to manually adjust it with an RZ gate, would that mean I can no longer batch since the RZ angle would be dependent on the number of negative amplitudes on the prepared state?
PS: Sorry for all the edits, I made a small mistake discuss the RZ phase angle offset correction