You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a list of slices are passed to the backpropagate method, they are processed in reverse order (last to first). However, if the slices are depth >1 then the instructions within the circuit are currently not being processed in reverse order.
Example 1
Say we have two slices, each made up of two instructions.
`slices' ~= [($U_1$, $U_2$), ($U_3$, $U_4$)]
When we backpropagate an observable $O$, we expect OBP to approximate $O' = (U^\dagger_1 U^\dagger_2) (U^\dagger_3 U^\dagger_4)\ O\ (U_4 U_3) (U_2 U_1)$
But because the instructions within each slice are not processed in reverse order what we actually compute is $O' = (U^\dagger_2 U^\dagger_1 U^\dagger_4 U^\dagger_3)\ O\ (U_3 U_4 U_1 U_2)$
Example 2
In the extreme case, which is reproduced below in code, circuits passed in as a single slice are forward-propagated entirely.
`slices' ~= [($U_1$, $U_2$, $U_3$, $U_4$)]
The current code will compute $O' = (U^\dagger_4 U^\dagger_3 U^\dagger_2 U^\dagger_1)\ O\ (U_1 U_2 U_3 U_4)$
How can we reproduce the issue?
import numpy as np
from copy import deepcopy
from qiskit import QuantumCircuit
from qiskit.compiler import transpile
from qiskit.quantum_info import SparsePauliOp, Operator
from qiskit_aer import AerSimulator
from qiskit_addon_obp import backpropagate
from qiskit_addon_obp.utils.simplify import OperatorBudget
from qiskit_addon_utils.slicing import slice_by_gate_types
O = SparsePauliOp.from_list([('IX',1),('XI',1),('XX',1),('IZ',1),('ZI',1),('ZZ',1)])
# Prepare the matrix representation of this observable
O_mat = O.to_matrix()
Compare $U^\dagger O U$ Computed with OBP vs computed directly with matrices
slices = slice_by_gate_types(circ)
op_budget = OperatorBudget()
# O' = U†•O•U computed via backprop
O_prime, remaining_slices, metadata = backpropagate(O, slices, operator_budget=op_budget)
O_prime_mat = O_prime.to_matrix()
# O' = U†•O•U computed directly via matrices
O_prime_mat_direct = unitary_mat.conj().T @ O_mat @ unitary_mat
print("These two matrices are equal, which is correct")
np.round(O_prime_mat_direct - O_prime_mat, 3)
>>>
These two matrices are equal, which is correct
array([[ 0.+0.j, 0.+0.j, 0.-0.j, -0.+0.j],
[ 0.+0.j, 0.+0.j, 0.-0.j, -0.+0.j],
[ 0.-0.j, 0.-0.j, -0.+0.j, 0.+0.j],
[-0.+0.j, -0.+0.j, 0.+0.j, -0.+0.j]])
Perform the same calculation but for OBP pass in a single slice with the whole circuit
op_budget = OperatorBudget()
# O' = U†•O•U computed via backprop
O_prime, remaining_slices, metadata = backpropagate(O, [circ], operator_budget=op_budget)
O_prime_mat = O_prime.to_matrix()
# O' = U†•O•U computed directly via matrices
O_prime_mat_direct = unitary_mat.conj().T @ O_mat @ unitary_mat
print("These two matrices are now not equal, which is Wrong")
np.round(O_prime_mat_direct - O_prime_mat, 3)
>>>
array([[-1.664-0.j, 1.371+0.j, 1.854-0.j, -0.707+0.j],
[ 1.371+0.j, -0.75 +0.j, 0.707+0.j, -0.146+0.j],
[ 1.854-0.j, 0.707+0.j, 0.457+0.j, 0.25 -0.j],
[-0.707+0.j, -0.146+0.j, 0.25 -0.j, 1.957-0.j]])
If instead we compare the output of OBP with the forward propagated operator $O* = U O U^\dagger$, we see agreement.
Environment
What is happening and why is it wrong?
What's wrong
When a list of slices are passed to the
backpropagate
method, they are processed in reverse order (last to first). However, if the slices are depth >1 then the instructions within the circuit are currently not being processed in reverse order.Example 1
Say we have two slices, each made up of two instructions.$U_1$ , $U_2$ ), ($U_3$ , $U_4$ )]
`slices' ~= [(
When we backpropagate an observable$O$ , we expect OBP to approximate
$O' = (U^\dagger_1 U^\dagger_2) (U^\dagger_3 U^\dagger_4)\ O\ (U_4 U_3) (U_2 U_1)$
But because the instructions within each slice are not processed in reverse order what we actually compute is
$O' = (U^\dagger_2 U^\dagger_1 U^\dagger_4 U^\dagger_3)\ O\ (U_3 U_4 U_1 U_2)$
Example 2
In the extreme case, which is reproduced below in code, circuits passed in as a single slice are forward-propagated entirely.
`slices' ~= [($U_1$ , $U_2$ , $U_3$ , $U_4$ )]
The current code will compute
$O' = (U^\dagger_4 U^\dagger_3 U^\dagger_2 U^\dagger_1)\ O\ (U_1 U_2 U_3 U_4)$
How can we reproduce the issue?
Define a circuit with an explicit unitary matrix
Define an observable$O$
Compare$U^\dagger O U$ Computed with OBP vs computed directly with matrices
Perform the same calculation but for OBP pass in a single slice with the whole circuit
If instead we compare the output of OBP with the forward propagated operator$O* = U O U^\dagger$ , we see agreement.
Traceback
No response
Any suggestions?
I think this can be addressed by changing Line 167 of
backpropagation.py
fromto
I realize that this casts the generator to a sequence in order to reverse it, so there likely is a more elegant solution.
The text was updated successfully, but these errors were encountered: