Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block encoding with matrix access oracles demo #943

Merged
merged 75 commits into from
Nov 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
dc4f974
add block encoding demo
soranjh Oct 2, 2023
5fa2126
add text for fable
soranjh Oct 2, 2023
54b109b
messy sparse block encoding section
Jaybsoni Oct 3, 2023
6a64406
add FABLE code blocks
DSGuala Oct 4, 2023
90aa2dd
add general circuit
soranjh Oct 4, 2023
feb6f3d
add general circuit
soranjh Oct 4, 2023
021873c
Edit fable code
DSGuala Oct 4, 2023
759828e
Merge branch 'add_block_encoding' of https://github.com/PennyLaneAI/q…
DSGuala Oct 4, 2023
91c2a6f
Merge branch 'master' into add_block_encoding
Jaybsoni Oct 4, 2023
6ee17ed
add better images
soranjh Oct 4, 2023
30d6919
Clean up section on block_encoding
Jaybsoni Oct 4, 2023
3353dd1
Merge branch 'add_block_encoding' of https://github.com/PennyLaneAI/q…
soranjh Oct 4, 2023
7fee215
add oracle definitions
DSGuala Oct 4, 2023
b2ec958
Merge branch 'add_block_encoding' of https://github.com/PennyLaneAI/q…
DSGuala Oct 4, 2023
fd7a6b6
Merge branch 'add_block_encoding' of https://github.com/PennyLaneAI/q…
soranjh Oct 4, 2023
faef859
edit text
soranjh Oct 4, 2023
eb6dcff
edit title
soranjh Oct 4, 2023
6115534
add demo to rst
soranjh Oct 4, 2023
6364893
add thumbnail image
soranjh Oct 5, 2023
be24402
add thumbnail image
soranjh Oct 5, 2023
6222b9f
add thumbnail image
soranjh Oct 5, 2023
c648b79
add thumbnail image
soranjh Oct 5, 2023
f1eace6
Merge branch 'master' into add_block_encoding
soranjh Oct 23, 2023
0a3a05a
modify text
soranjh Oct 23, 2023
5053a2f
modify text
soranjh Oct 27, 2023
e017d98
modify text and code
soranjh Oct 27, 2023
11f0e4c
clean up code
soranjh Oct 27, 2023
8509dd4
merge master and fix conflict
soranjh Oct 27, 2023
d27315e
correct code
soranjh Oct 27, 2023
e113836
modify print
soranjh Oct 30, 2023
13beb83
Merge branch 'master' into add_block_encoding
Jaybsoni Nov 1, 2023
a4fdd0f
Added more theory
Jaybsoni Nov 2, 2023
24709c6
Merge branch 'master' into add_block_encoding
Jaybsoni Nov 2, 2023
05ef7a8
re-word conclusion
Jaybsoni Nov 2, 2023
c291518
Apply suggestions from code review
Jaybsoni Nov 6, 2023
cd74556
Merge branch 'master' into add_block_encoding
Jaybsoni Nov 6, 2023
ff5c3f1
update text
soranjh Nov 8, 2023
3470a2a
add input args
soranjh Nov 8, 2023
6d630f1
add review comments
soranjh Nov 8, 2023
d925a4e
correct code
soranjh Nov 8, 2023
b763630
update metadata
soranjh Nov 8, 2023
1c7cbf0
update rst file
soranjh Nov 8, 2023
be74146
address review comments
Jaybsoni Nov 9, 2023
9bc5f3a
more code review comments
Jaybsoni Nov 9, 2023
4cc5d20
FABLE: change oracle signatures, allow arbitrary matrix size
DSGuala Nov 9, 2023
8e06744
Merge branch 'add_block_encoding' of https://github.com/PennyLaneAI/q…
DSGuala Nov 9, 2023
f6f17ca
update code
soranjh Nov 9, 2023
e440af5
modify code
soranjh Nov 9, 2023
5080287
update images
DSGuala Nov 10, 2023
2497e7b
fix wire labeling and make uniform
Jaybsoni Nov 10, 2023
77cf815
correct text
soranjh Nov 10, 2023
d8e390b
Merge branch 'add_block_encoding' of https://github.com/PennyLaneAI/q…
soranjh Nov 10, 2023
7f18d67
modify text and code
soranjh Nov 10, 2023
7fcf793
Update demonstrations/tutorial_block_encoding.py
Jaybsoni Nov 13, 2023
6883b07
add small corrections
soranjh Nov 13, 2023
9c77c9a
Merge branch 'add_block_encoding' of https://github.com/PennyLaneAI/q…
soranjh Nov 13, 2023
efe409a
add small corrections
soranjh Nov 13, 2023
f195407
review comments + new thumbnail
Jaybsoni Nov 16, 2023
cc1fa9e
missed rst file
Jaybsoni Nov 16, 2023
047a229
code review comments
Jaybsoni Nov 16, 2023
66db3d0
Merge branch 'master' into add_block_encoding
Jaybsoni Nov 16, 2023
564b311
changing large thumbnail path
KetpuntoG Nov 16, 2023
3e04701
empty commit to redeploy
KetpuntoG Nov 16, 2023
aadd90e
smalls modifications
KetpuntoG Nov 17, 2023
761f663
Update tutorial_block_encoding.py
KetpuntoG Nov 17, 2023
c38619d
update related demos
KetpuntoG Nov 17, 2023
988564b
remove theory
Jaybsoni Nov 17, 2023
a57dc3a
add theory
Jaybsoni Nov 21, 2023
682d80a
add anc to image
DSGuala Nov 21, 2023
b7686fc
clean up explanation
Jaybsoni Nov 23, 2023
fa49d61
Update demonstrations/tutorial_block_encoding.py
Jaybsoni Nov 23, 2023
54e9d6d
fix sentence
Jaybsoni Nov 23, 2023
4b305bb
Apply suggestions from code review
soranjh Nov 24, 2023
e5c175b
make circuit compact
soranjh Nov 24, 2023
dcb9146
Apply suggestions from code review
Jaybsoni Nov 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add small corrections
soranjh committed Nov 13, 2023
commit efe409af159dd87725eeb19d88e0a720d7c04700
21 changes: 11 additions & 10 deletions demonstrations/tutorial_block_encoding.py
Original file line number Diff line number Diff line change
@@ -23,8 +23,8 @@
(LCU) decompositions. In this tutorial we explore another general block encoding method that can be
very efficient for sparse and structured matrices.

Circuits with matrix access oracles [#fable, #sparse]_ can block encode an arbitrary matrix
:math:`A`. These circuits can be constructed as shown in the figure below.
Circuits with matrix access oracles, [#fable]_ [#sparse]_, can block encode an arbitrary
matrix :math:`A`. These circuits can be constructed as shown in the figure below.

.. figure:: ../demonstrations/block_encoding/general_circuit.png
:width: 50%
soranjh marked this conversation as resolved.
Show resolved Hide resolved
@@ -138,7 +138,7 @@ def circuit():
# compare it with the original matrix.

print(f"Original matrix:\n{A}", "\n")
wire_order = [ancilla_wire] + wires_i[::-1] + wires_j[::-1]
wire_order = [ancilla_wire] + wires_i[::-1] + wires_j[::-1]
M = len(A) * qml.matrix(circuit, wire_order=wire_order)().real[0:len(A),0:len(A)]
print(f"Block-encoded matrix:\n{M}", "\n")

@@ -157,24 +157,25 @@ def circuit():

def UA(thetas, control_wires, ancilla):
for theta, control_index in zip(thetas, control_wires):
if abs(theta)>tolerance:
qml.RY(theta, wires=ancilla)
if abs(2 * theta)>tolerance:
qml.RY(2 * theta, wires=ancilla)
qml.CNOT(wires=[wire_map[control_index], ancilla])

qml.draw_mpl(circuit, style='pennylane')()
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved

##############################################################################
# Compressing the circuit by removing some of the rotations is an approximation. We can now remove
# the C-NOT gates that cancel each other out and see how good this approximation is in the case
# of our example.
# of our example. We will make a small modification to :math:`U_A` so that it removes those
# C-NOT gates that cancel each other out.

def UA(thetas, control_wires, ancilla):
nots=[]
for theta, control_index in zip(thetas, control_wires):
if abs(theta) > tolerance:
if abs(2 * theta) > tolerance:
for c_wire in nots:
qml.CNOT(wires=[c_wire, ancilla])
qml.RY(theta,wires=ancilla)
qml.RY(2 * theta,wires=ancilla)
nots=[]
if (cw := wire_map[control_index]) in nots:
del(nots[nots.index(cw)])
@@ -299,8 +300,8 @@ def complete_circuit(thetas):

##############################################################################
# You can confirm that the circuit block encodes the original sparse matrix defined above.
# Note that if we wanted to increase the dimension of A (for example :math:`16 \times 16`), then we
# need only to add more wires to the ``j`` register in the device and :code:`UB`.
# Note that if we wanted to increase the dimension of :math:`A` (for example :math:`16 \times 16`),
# then we need only to add more wires to the ``j`` register in the device and in :code:`UB`.
#
# Conclusion
# -----------------------