BlockEncoding.__sub__#

BlockEncoding.__sub__(other: BlockEncoding) BlockEncoding[source]#

Returns a BlockEncoding of the difference between two operators.

This method implements the subtraction \(A - B\) using a linear combination of unitaries (LCU), where \(A\) is the operator encoded by this instance and \(B\) is the operator encoded by ‘other’.

Parameters:
otherBlockEncoding

The BlockEncoding instance to be subtracted.

Returns:
BlockEncoding

A new BlockEncoding representing the operator difference.

Notes

  • Can only be used when both BlockEncodings have the same operand structure.

  • The - operator should be used sparingly, primarily to combine a few block encodings. For larger-scale polynomial transformations, Quantum Signal Processing (QSP) is the superior method.

Examples

Define two block-encodings and subtract them.

from qrisp import *
from qrisp.block_encodings import BlockEncoding
from qrisp.operators import X, Y, Z

H1 = X(0)*X(1) + 0.2*Y(0)*Y(1)
H2 = Z(0)*Z(1) + X(2)
H3 = H1 - H2

BE1 = BlockEncoding.from_operator(H1)
BE2 = BlockEncoding.from_operator(H2)
BE3 = BlockEncoding.from_operator(H3)

BE_sub = BE1 - BE2

def operand_prep():
    qv = QuantumFloat(3)
    return qv

@terminal_sampling
def main(BE):
    qv = BE.apply_rus(operand_prep)()
    return qv

res_be3 = main(BE3)
res_be_sub = main(BE_sub)
print("Result from BE of H1 - H2: ", res_be3)
print("Result from BE1 - BE2: ", res_be_sub)
# Result from BE of H1 - H2:  {0: 0.37878788804466035, 4: 0.37878788804466035, 3: 0.24242422391067933}
# Result from BE1 - BE2:  {0: 0.37878789933341894, 4: 0.37878789933341894, 3: 0.24242420133316217}