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

Use Boolean Hamiltonian Gate for QAOA #3989

Closed
wants to merge 190 commits into from
Closed
Show file tree
Hide file tree
Changes from 164 commits
Commits
Show all changes
190 commits
Select commit Hold shift + click to select a range
914f3d5
Hamiltonian representation of binary functions
tonybruguier Apr 3, 2021
a0c19ef
Allow for multiple expressions
tonybruguier Apr 3, 2021
9192792
De-dupe names
tonybruguier Apr 3, 2021
739734b
Add to existing circuit
tonybruguier Apr 3, 2021
579dc33
Add to existing circuit
tonybruguier Apr 3, 2021
b89192a
Merge branch 'hamiltonian' of github.com:tonybruguier/Cirq into hamil…
tonybruguier Apr 4, 2021
78d3969
fix coverage and formatting
tonybruguier Apr 4, 2021
499083e
add missing file
tonybruguier Apr 4, 2021
5251a78
Modify QAOA
tonybruguier Apr 4, 2021
715a7dd
Attempt to fix unit test
tonybruguier Apr 4, 2021
70ac1df
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 6, 2021
b812040
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 7, 2021
34a1d08
Sort indices
tonybruguier Apr 7, 2021
db6a261
More comments
tonybruguier Apr 7, 2021
99f33b2
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 8, 2021
3bf90e6
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 10, 2021
f2fa497
Address comments
tonybruguier Apr 12, 2021
0e1980f
Make float for weights
tonybruguier Apr 12, 2021
e7c99ec
Attempt to mypy fix
tonybruguier Apr 12, 2021
d8ede30
Faster exec
tonybruguier Apr 12, 2021
078f91a
Better test coverage
tonybruguier Apr 12, 2021
4954bc4
nit add missing \n
tonybruguier Apr 12, 2021
ae20cdf
Address more comments
tonybruguier Apr 12, 2021
f757a77
mypy attempt at fixing
tonybruguier Apr 12, 2021
05d8972
Not a default dict
tonybruguier Apr 12, 2021
8cd10df
mypy
tonybruguier Apr 12, 2021
48d3ba4
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 13, 2021
4cadd86
Address comments
tonybruguier Apr 13, 2021
71e4316
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 13, 2021
bb59cb9
Address comments
tonybruguier Apr 13, 2021
c1ce450
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 15, 2021
a36f8bb
more unit tests
tonybruguier Apr 15, 2021
a2641fc
Even more tests
tonybruguier Apr 15, 2021
42ecdd3
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 15, 2021
9d6fbae
Add an alternate way of building the CNOTs
tonybruguier Apr 16, 2021
9b64e6c
mypy
tonybruguier Apr 16, 2021
007eb2e
lint
tonybruguier Apr 16, 2021
d9fc1b6
Add some simplification
tonybruguier Apr 16, 2021
abce039
Re-add missing coverage
tonybruguier Apr 16, 2021
5e155d9
nits
tonybruguier Apr 16, 2021
f199f1f
Implement equation 11.
tonybruguier Apr 17, 2021
a2aeb50
Merge branch 'master' into hamiltonian
tonybruguier Apr 19, 2021
167b4f5
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 20, 2021
7057d9b
Address comments
tonybruguier Apr 20, 2021
89239e4
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 21, 2021
b487c06
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 22, 2021
dfc3ffc
Address comments
tonybruguier Apr 22, 2021
9f0a416
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 23, 2021
99a3dc5
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Apr 28, 2021
152a731
Merge branch 'master' into hamiltonian
tonybruguier May 3, 2021
ad62478
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 4, 2021
05d8268
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 5, 2021
5338e61
Add a TODO
tonybruguier May 5, 2021
473fa2b
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 6, 2021
c18bec6
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 8, 2021
d110091
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 11, 2021
1524845
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 11, 2021
5372cab
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 15, 2021
cb6da19
Revert some files
tonybruguier May 15, 2021
167a1db
Update examples/hamiltonian_representation.py
tonybruguier May 15, 2021
6403294
Rename file
tonybruguier May 15, 2021
f4e7658
Rename file
tonybruguier May 15, 2021
396ea47
Make a gate
tonybruguier May 15, 2021
a3d7291
Add gate
tonybruguier May 15, 2021
21ff825
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 18, 2021
fac49e3
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 20, 2021
4a994c4
Address some comments
tonybruguier May 20, 2021
1105d2b
Rename gate
tonybruguier May 20, 2021
d9f462f
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 21, 2021
6cd9366
execute TODO
tonybruguier May 21, 2021
4dea601
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 21, 2021
a0c700f
More unit tests
tonybruguier May 21, 2021
dacf48f
nits
tonybruguier May 23, 2021
28c96ce
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 25, 2021
6821aaa
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 27, 2021
96422c5
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 28, 2021
d0d5f25
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier May 28, 2021
997ac56
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 3, 2021
57c0f7a
Update cirq-core/cirq/ops/boolean_hamiltonian_operation.py
tonybruguier Jun 4, 2021
09c98d3
Update cirq-core/cirq/ops/boolean_hamiltonian_operation.py
tonybruguier Jun 4, 2021
3a3ad6a
Update cirq-core/cirq/ops/boolean_hamiltonian_operation.py
tonybruguier Jun 4, 2021
06a2ccc
Merge branch 'master' into hamiltonian
tonybruguier Jun 4, 2021
47bb1a1
Update cirq-core/cirq/ops/boolean_hamiltonian_operation.py
tonybruguier Jun 4, 2021
4a9cba4
nit
tonybruguier Jun 4, 2021
20b5896
Rename BooleanHamiltonianOperation to BooleanHamiltonian
tonybruguier Jun 4, 2021
15f1ae0
Merge branch 'hamiltonian' of github.com:tonybruguier/Cirq into hamil…
tonybruguier Jun 4, 2021
789d656
fix some errors, hopefully
tonybruguier Jun 4, 2021
83a2fff
fix import
tonybruguier Jun 4, 2021
e7340a9
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 5, 2021
c513e47
New base class
tonybruguier Jun 5, 2021
7a510b7
Make get_name_to_id() private
tonybruguier Jun 5, 2021
ccf429c
Use PauliString
tonybruguier Jun 5, 2021
e93bd01
Some fixes
tonybruguier Jun 5, 2021
f46efe8
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 7, 2021
51d2478
de-brittle unit test
tonybruguier Jun 7, 2021
e1069cf
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 10, 2021
03a5226
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 11, 2021
fa3b754
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 12, 2021
3aadaa5
Intermediate addressing of comments
tonybruguier Jun 12, 2021
17086bc
Address more comments
tonybruguier Jun 12, 2021
df9e733
Attempt to fix types
tonybruguier Jun 12, 2021
37f791c
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 13, 2021
5252349
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 14, 2021
e3e783c
Update cirq-core/cirq/ops/boolean_hamiltonian.py
tonybruguier Jun 15, 2021
67a8bb8
Update cirq-core/cirq/ops/boolean_hamiltonian.py
tonybruguier Jun 15, 2021
ce57671
Update cirq-core/cirq/ops/boolean_hamiltonian.py
tonybruguier Jun 15, 2021
2e33032
Add some unit tests. More to do.
tonybruguier Jun 15, 2021
609cf08
Merge branch 'hamiltonian' of github.com:tonybruguier/Cirq into hamil…
tonybruguier Jun 15, 2021
d45e5de
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 15, 2021
778a4c2
Address more comments
tonybruguier Jun 15, 2021
1cbb63f
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 16, 2021
f7cdf12
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 17, 2021
5bad288
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 18, 2021
e7e6399
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 19, 2021
bb06b8a
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 21, 2021
e295225
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 22, 2021
2a6ffde
Update cirq-core/cirq/ops/boolean_hamiltonian.py
tonybruguier Jun 22, 2021
08cac1a
Update examples/qaoa.py
tonybruguier Jun 22, 2021
ce98939
Update examples/qaoa.py
tonybruguier Jun 22, 2021
a02b6f2
Update cirq-core/cirq/ops/boolean_hamiltonian.py
tonybruguier Jun 22, 2021
0afb1f4
Address some comments
tonybruguier Jun 22, 2021
1ea6b91
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 22, 2021
2362d9d
Add back the examples
tonybruguier Jun 22, 2021
661504e
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 22, 2021
1505292
Merge branch 'hamiltonian' of github.com:tonybruguier/Cirq into hamil…
tonybruguier Jun 22, 2021
06e1c03
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 23, 2021
138795d
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 25, 2021
a06dfa4
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 26, 2021
dbb0275
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 28, 2021
9457433
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jun 30, 2021
2baf674
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 1, 2021
a6b1c8e
Update cirq-core/cirq/ops/boolean_hamiltonian.py
tonybruguier Jul 2, 2021
3fe8a12
Find min gate num
tonybruguier Jul 2, 2021
a296051
Merge branch 'hamiltonian' of github.com:tonybruguier/Cirq into hamil…
tonybruguier Jul 2, 2021
cfea2a7
Update cirq-core/cirq/ops/boolean_hamiltonian.py
tonybruguier Jul 2, 2021
4354469
nit
tonybruguier Jul 2, 2021
de8463f
attempt fix mypy
tonybruguier Jul 2, 2021
03c3281
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 2, 2021
c60c522
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 3, 2021
4ba558e
Remove ladder
tonybruguier Jul 3, 2021
a8687a6
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 4, 2021
4b6b912
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 6, 2021
5446463
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 8, 2021
5611632
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 9, 2021
bfa68d4
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 9, 2021
d2e9ce8
Merge changes
tonybruguier Jul 9, 2021
e93b472
Actually call simplification code
tonybruguier Jul 9, 2021
48e2722
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 10, 2021
394c376
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 14, 2021
af95a3a
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 15, 2021
94eb92d
nit
tonybruguier Jul 15, 2021
45c6b79
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 17, 2021
9fcd3ac
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 20, 2021
5443558
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 20, 2021
26a4ec7
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 22, 2021
e998615
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 24, 2021
3d74c02
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 25, 2021
05a7996
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 28, 2021
874914f
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 28, 2021
78b66da
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Jul 31, 2021
2f5f127
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 5, 2021
51afec4
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 6, 2021
0e1504d
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 6, 2021
f3149a1
Remove boolean hamiltonian
tonybruguier Aug 6, 2021
abd3a64
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 8, 2021
52ed083
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 9, 2021
42662c9
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 10, 2021
b887282
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 11, 2021
53bb242
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 12, 2021
5db505d
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 14, 2021
1542069
Obliterate diffs so that I can start over
tonybruguier Aug 14, 2021
277850f
Refactor to use both approaches
tonybruguier Aug 14, 2021
66ec921
Nits
tonybruguier Aug 14, 2021
d33bbe3
nit
tonybruguier Aug 14, 2021
b728fa7
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 17, 2021
90e3b65
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 18, 2021
fcb12ad
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 19, 2021
9acd5d8
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 21, 2021
dce65cd
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 26, 2021
b7c19d3
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Aug 29, 2021
084c804
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Sep 2, 2021
28ebd17
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Sep 8, 2021
c76b9fd
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Sep 16, 2021
fe8bd8b
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Sep 25, 2021
fbce71e
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Sep 29, 2021
f587e60
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Oct 2, 2021
854622f
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Oct 12, 2021
21da5a1
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Oct 21, 2021
78de9e4
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Oct 29, 2021
eaee8f4
Merge branch 'master' of github.com:quantumlib/Cirq into hamiltonian
tonybruguier Mar 18, 2022
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
2 changes: 1 addition & 1 deletion examples/examples_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_example_heatmaps():


def test_example_runs_qaoa():
examples.qaoa.main(repetitions=10, maxiter=5)
examples.qaoa.main(repetitions=1, maxiter=1, p=1)


def test_example_runs_quantum_teleportation():
Expand Down
249 changes: 128 additions & 121 deletions examples/qaoa.py
Original file line number Diff line number Diff line change
@@ -1,155 +1,162 @@
"""Runs the Quantum Approximate Optimization Algorithm on Max-Cut.
tonybruguier marked this conversation as resolved.
Show resolved Hide resolved
tonybruguier marked this conversation as resolved.
Show resolved Hide resolved

=== EXAMPLE OUTPUT ===
=== EXAMPLE CIRCUIT ===
x0 x1 x2 x3
│ │ │ │
H H H H
│ │ │ │
│ │ Y^-0.5 Y^-0.5
│ │ │ │
│ @─────@ │
│ │ │ │
│ │ Y^0.5 │
│ │ │ │
│ │ Rz(0) │
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just noticing these Rz(0), Rx(0) gates - we should filter these out I think at least in the example by passing the circuit through a cirq.DropNegligible().optimize_circuit(circuit).

│ │ │ │
│ │ Y^-0.5 │
│ │ │ │
│ @─────@ │
│ │ │ │
┌╴│ │ │ │ ╶┐
│ │ │ Y^0.5 │ │
│ │ @─────┼──────@ │
└╴│ │ │ │ ╶┘
│ │ │ │
│ │ Y^-0.5 Y^0.5
│ │ │ │
@─────┼─────@ Rz(0)
│ │ │ │
│ │ Y^0.5 Y^-0.5
│ │ │ │
┌╴│ │ │ │ ╶┐
│ │ │ Rz(0) │ │
│ │ @─────┼──────@ │
└╴│ │ │ │ ╶┘
│ │ │ │
│ Rx(0) Y^-0.5 Y^0.5
│ │ │ │
@─────┼─────@ Y^-0.5
│ │ │ │
┌╴│ │ │ │ ╶┐
│ │ │ Y^0.5 │ │
│ @─────┼─────┼──────@ │
└╴│ │ │ │ ╶┘
│ │ │ │
│ │ Rx(0) Y^0.5
│ │ │ │
│ │ │ Rz(0)
│ │ │ │
│ │ │ Y^-0.5
│ │ │ │
@─────┼─────┼──────@
│ │ │ │
Rx(0) │ │ Y^0.5
│ │ │ │
│ │ │ Rx(0)
│ │ │ │
M─────M─────M──────M('m')
│ │ │ │

Example QAOA circuit:
tonybruguier marked this conversation as resolved.
Show resolved Hide resolved
0 1 2 3 4 5
│ │ │ │ │ │
H H H H H H
│ │ │ │ │ │
ZZ──────────ZZ^(-4/13) │ │ │ │
┌ │ │ │ │ │ │ ┐
│ ZZ──────────┼───────────ZZ^(-4/13) │ │ │ │
│ │ ZZ──────────┼───────────ZZ^(-4/13) │ │ │
└ │ │ │ │ │ │ ┘
┌ │ │ │ │ │ │ ┐
│ ZZ──────────┼───────────┼───────────┼───────────ZZ^(-4/13) │ │
│ │ ZZ──────────┼───────────┼───────────┼───────────ZZ^(-4/13) │
└ │ │ │ │ │ │ ┘
Rx(0.151π) Rx(0.151π) ZZ──────────┼───────────ZZ^(-4/13) │
│ │ │ │ │ │
ZZ──────────ZZ^-0.941 ZZ──────────┼───────────┼───────────ZZ^(-4/13)
│ │ │ ZZ──────────ZZ^(-4/13) │
┌ │ │ │ │ │ │ ┐
│ │ │ Rx(0.151π) ZZ──────────┼───────────ZZ^(-4/13) │
│ │ │ │ │ Rx(0.151π) │ │
└ │ │ │ │ │ │ ┘
ZZ──────────┼───────────ZZ^-0.941 Rx(0.151π) │ Rx(0.151π)
┌ │ │ │ │ │ │ ┐
│ ZZ──────────┼───────────┼───────────┼───────────ZZ^-0.941 │ │
│ │ ZZ──────────┼───────────ZZ^-0.941 │ │ │
└ │ │ │ │ │ │ ┘
Rx(-0.448π) ZZ──────────┼───────────┼───────────┼───────────ZZ^-0.941
│ │ ZZ──────────┼───────────ZZ^-0.941 │
│ │ │ │ │ │
│ Rx(-0.448π) ZZ──────────┼───────────┼───────────ZZ^-0.941
│ │ │ ZZ──────────ZZ^-0.941 │
┌ │ │ │ │ │ │ ┐
│ │ │ Rx(-0.448π) ZZ──────────┼───────────ZZ^-0.941 │
│ │ │ │ │ Rx(-0.448π) │ │
└ │ │ │ │ │ │ ┘
│ │ │ Rx(-0.448π) │ Rx(-0.448π)
│ │ │ │ │ │
M('m')──────M───────────M───────────M───────────M───────────M
│ │ │ │ │ │
Optimizing objective function ...
The largest cut value found was 7.
The largest possible cut has size 7.
The approximation ratio achieved is 1.0.
=== EXAMPLE OUTPUT ===
Brute force max cut: 16
μ=7.80 max=12
μ=7.40 max=8
μ=8.60 max=10
μ=8.00 max=10
μ=8.80 max=12
μ=8.20 max=12
μ=10.00 max=16
μ=8.00 max=16
μ=4.40 max=12
μ=5.80 max=12

Return from subroutine COBYLA because the MAXFUN limit has been reached.

NFVALS = 10 F =-1.000000E+01 MAXCV = 0.000000E+00
X = 0.000000E+00 1.000000E+00 0.000000E+00 1.000000E+00 0.000000E+00
1.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00
"""

import itertools
from typing import List

import numpy as np
import networkx
import scipy.optimize
from sympy.parsing.sympy_parser import parse_expr

import cirq


def main(repetitions=1000, maxiter=50):
# Set problem parameters
n = 6
p = 2
def brute_force(graph, n):
bitstrings = np.array(list(itertools.product(range(2), repeat=n)))
mat = networkx.adjacency_matrix(graph, nodelist=sorted(graph.nodes))
vecs = (-1) ** bitstrings
vals = 0.5 * np.sum(vecs * (mat @ vecs.T).T, axis=-1)
vals = 0.5 * (graph.size() - vals)
return max(np.round(vals))

# Generate a random 3-regular graph on n nodes
graph = networkx.random_regular_graph(3, n)

# Make qubits
qubits = cirq.LineQubit.range(n)
def qaoa(booleans: List[str], repetitions: int, maxiter: int, p: int):
"""Run the QAOA optimization for a list of Boolean expressions.

# Print an example circuit
betas = np.random.uniform(-np.pi, np.pi, size=p)
gammas = np.random.uniform(-np.pi, np.pi, size=p)
circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph)
print('Example QAOA circuit:')
print(circuit.to_text_diagram(transpose=True))
Args:
booleans: A list of Boolean expressions (we want as many of them to be true as possible).
repetitions: The number of times to repeat the measurements.
maxiter: The number of iterations of the optimizer.
p: The number of times to repeat the Hamiltonian gate.
"""
boolean_exprs = [parse_expr(boolean) for boolean in booleans]
param_names = cirq.parameter_names(boolean_exprs)
qubits = [cirq.NamedQubit(name) for name in param_names]

# Create variables to store the largest cut and cut value found
largest_cut_found = None
largest_cut_value_found = 0
def f(x):
# Build the circuit.
circuit = cirq.Circuit()
circuit.append(cirq.H.on_each(*qubits))

# Initialize simulator
simulator = cirq.Simulator()
for i in range(p):
hamiltonian_gate = cirq.BooleanHamiltonian(
{q.name: q for q in qubits}, booleans, 2.0 * x[p + i]
)
circuit.append(hamiltonian_gate)
circuit.append(cirq.rx(2.0 * x[i]).on_each(*qubits))

# Define objective function (we'll use the negative expected cut value)
circuit.append(cirq.measure(*qubits, key='m'))

def f(x):
# Create circuit
betas = x[:p]
gammas = x[p:]
circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph)
# Sample bitstrings from circuit
result = simulator.run(circuit, repetitions=repetitions)
# Measure
result = cirq.Simulator().run(circuit, repetitions=repetitions)
bitstrings = result.measurements['m']
# Process bitstrings
nonlocal largest_cut_found
nonlocal largest_cut_value_found
values = cut_values(bitstrings, graph)
max_value_index = np.argmax(values)
max_value = values[max_value_index]
if max_value > largest_cut_value_found:
largest_cut_value_found = max_value
largest_cut_found = bitstrings[max_value_index]
mean = np.mean(values)
return -mean

# Pick an initial guess
x0 = np.random.uniform(-np.pi, np.pi, size=2 * p)

# Optimize f
print('Optimizing objective function ...')
scipy.optimize.minimize(f, x0, method='Nelder-Mead', options={'maxiter': maxiter})

# Compute best possible cut value via brute force search
all_bitstrings = np.array(list(itertools.product(range(2), repeat=n)))
all_values = cut_values(all_bitstrings, graph)
max_cut_value = np.max(all_values)
# Evaluate
values = []
for rep in range(repetitions):
subs = {name: val == 1 for name, val in zip(param_names, bitstrings[rep, :])}
values.append(
sum(1 if boolean_expr.subs(subs) else 0 for boolean_expr in boolean_exprs)
)

# Print the results
print(f'The largest cut value found was {largest_cut_value_found}.')
print(f'The largest possible cut has size {max_cut_value}.')
print(f'The approximation ratio achieved is {largest_cut_value_found / max_cut_value}.')
print('μ=%.2f max=%d' % (np.mean(values), max(values)))

return -np.mean(values)

def rzz(rads):
"""Returns a gate with the matrix exp(-i Z⊗Z rads)."""
return cirq.ZZPowGate(exponent=2 * rads / np.pi, global_shift=-0.5)
x0 = np.zeros(2 * p)
scipy.optimize.minimize(f, x0, method='COBYLA', options={'maxiter': maxiter, 'disp': True})


def qaoa_max_cut_unitary(qubits, betas, gammas, graph): # Nodes should be integers
for beta, gamma in zip(betas, gammas):
yield (rzz(-0.5 * gamma).on(qubits[i], qubits[j]) for i, j in graph.edges)
yield cirq.rx(2 * beta).on_each(*qubits)
def main(repetitions=10, maxiter=250, p=5):
# Set problem parameters
n = 6

# Generate a random bipartite graph.
graph = networkx.complete_multipartite_graph(n, n)

def qaoa_max_cut_circuit(qubits, betas, gammas, graph): # Nodes should be integers
return cirq.Circuit(
# Prepare uniform superposition
cirq.H.on_each(*qubits),
# Apply QAOA unitary
qaoa_max_cut_unitary(qubits, betas, gammas, graph),
# Measure
cirq.measure(*qubits, key='m'),
)
# Compute best possible cut value via brute force search
print('Brute force max cut: %d' % (brute_force(graph, 2 * n)))

# Build the boolean expressions
booleans = [f"x{i} ^ x{j}" for i, j in graph.edges]

def cut_values(bitstrings, graph):
mat = networkx.adjacency_matrix(graph, nodelist=sorted(graph.nodes))
vecs = (-1) ** bitstrings
vals = 0.5 * np.sum(vecs * (mat @ vecs.T).T, axis=-1)
vals = 0.5 * (graph.size() - vals)
return vals
qaoa(booleans, repetitions=repetitions, maxiter=maxiter, p=p)


if __name__ == '__main__':
Expand Down