Skip to content

Use Boolean Hamiltonian gates for QAOA example #5098

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

Merged
merged 121 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 120 commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
e38f1f0
Use Boolean Hamiltonian gates for QAOA example
tonybruguier Mar 18, 2022
52b1f0d
Don't use deprecated function
tonybruguier Mar 18, 2022
a2116c6
More deprecation
tonybruguier Mar 18, 2022
da2577c
Deprectate log_of_measurement_result input (#5100)
daxfohl Mar 18, 2022
75b39c0
Improve support for recursively applying transformer primitives on ci…
tanujkhattar Mar 21, 2022
db3f132
Add support for deep=True flag to remaining transformer primitives (#…
tanujkhattar Mar 21, 2022
595586f
Add `assert_decompose_ends_at_default_gateset` to consistent protocol…
tanujkhattar Mar 21, 2022
c8364f7
Extract BufferedDM/SV/MPS from ActOnDM/SV/MPSArgs (#4979)
daxfohl Mar 21, 2022
1aebfc5
Bugfixes in handling nested tags_to_ignore + deep=True in `cirq.map_m…
tanujkhattar Mar 21, 2022
068ab2f
Add `add_deep_support ` flag to `@cirq.transformer` decorator (#5108)
tanujkhattar Mar 21, 2022
96aa4c1
Add support for deep=True to `cirq.drop_empty_moments` transformer (#…
tanujkhattar Mar 21, 2022
0b9de59
Add support for deep=True to `cirq.drop_negligible_operations` transf…
tanujkhattar Mar 21, 2022
f215480
Add support for deep=True to `cirq.stratified_circuit` transformer (#…
tanujkhattar Mar 21, 2022
d9fed98
Add support for deep=True to `cirq.synchronize_terminal_measurements`…
tanujkhattar Mar 21, 2022
c2038c2
Add support for deep=True to `cirq.expand_composite` transformer (#5119)
tanujkhattar Mar 21, 2022
4abb225
Add support for deep=True to `cirq.eject_phased_paulis` transformer (…
tanujkhattar Mar 21, 2022
3f5669b
Add support for deep=True to `cirq.align_left` and `cirq.align_right`…
tanujkhattar Mar 22, 2022
4a5c138
Add support for deep=True to `cirq.eject_z` transformer (#5115)
tanujkhattar Mar 22, 2022
4b237e0
Add support for deep=True to cirq.merge_k_qubit_unitaries transformer…
tanujkhattar Mar 22, 2022
d19c057
Add support for deep=True to merge_single_qubit_gates* transformers (…
tanujkhattar Mar 22, 2022
0ed4978
Add support for deep=True to `cirq.optimize_for_target_gateset` trans…
tanujkhattar Mar 22, 2022
626f5ac
Bugfix in handling of deep=True flag in `cirq.merge_k_qubit_unitaries…
tanujkhattar Mar 22, 2022
a449b9b
DeviceSpecification proto updates (#5056)
verult Mar 22, 2022
d927f52
Add support for deep=True flag in `cg.optimized_for_sycamore` and `cg…
tanujkhattar Mar 22, 2022
693a9d8
Rename TestDevice -> FakeDevice to avoid pytest confusion (#5066)
maffoo Mar 22, 2022
54d4603
Add iterator support for AbstractJob (#5136)
dstrain115 Mar 24, 2022
9e19cd7
Pin Jinja2 version for build_docs CI. (#5138)
MichaelBroughton Mar 24, 2022
be83e0b
Bump cirq version to 0.15.0 (#5134)
MichaelBroughton Mar 24, 2022
e05d636
Bump minimist from 1.2.5 to 1.2.6 in /cirq-web/cirq_ts (#5140)
dependabot[bot] Mar 24, 2022
ee3e7ca
Remove deprecated two_qubit_matrix_to_diagonal_and_operations and two…
tonybruguier Mar 25, 2022
e994b89
Fix broken caching in CliffordGate and add test (#5142)
dabacon Mar 25, 2022
30aba7b
Cleanup docs/noise.ipynb in preparation for Cirq 1.0 launch (#5147)
tanujkhattar Mar 25, 2022
bed698a
Speed up a slow transformer test (#5146)
dabacon Mar 25, 2022
4ee4db9
Make pylint parallel (#5144)
dabacon Mar 25, 2022
1b824b6
[workflow] Preliminary timing information (#5021)
mpharrigan Mar 26, 2022
5a01024
Add docstring for `cirq.TRANSFORMER` public object (#5149)
tanujkhattar Mar 28, 2022
ffa722a
Base class for quantum states (#5065)
daxfohl Mar 28, 2022
8f0f4d5
Allow any object with supporting protocols to be the action in act_on…
daxfohl Mar 28, 2022
c03e347
Fix raiser is not callable. (#5155)
MichaelBroughton Mar 28, 2022
137ddb6
Corrected result.data implementation. (#5153)
MichaelBroughton Mar 28, 2022
551acea
Support specific IonQ targets (qpu.generation) (#5141)
Cynocracy Mar 28, 2022
b4dc6d1
Upgrade black to stable version with format stability guarantees (#5157)
maffoo Mar 29, 2022
a6e0c79
Format cirq-core with latest version of black (#5159)
maffoo Mar 29, 2022
6603ae5
Clarify virtual tag docstring (#5161)
dabacon Mar 29, 2022
43f0581
Format cirq-google with latest version of black (#5160)
maffoo Mar 29, 2022
310e953
Ignore large-scale formatting changes for git blame (#5162)
maffoo Mar 29, 2022
2ba3491
Remove --pre from notebooks after release. (#5135)
MichaelBroughton Mar 29, 2022
6371600
Update to pylint 2.13 (#5156)
maffoo Mar 29, 2022
a133b06
Update educators/intro.ipynb in preparation for Cirq 1.0 launch (#5163)
tanujkhattar Mar 30, 2022
fa80649
Disable broken symbols. (#5165)
MichaelBroughton Mar 30, 2022
9a2d4d8
Autoformat all of engine_client_test.py (#5164)
maffoo Mar 30, 2022
37a3e84
Allow specifying timeout_seconds when constructing an ionq Sampler (#…
Cynocracy Mar 31, 2022
872903e
Remove reservation colab (#5168)
dstrain115 Mar 31, 2022
e28a216
Add Google-specific variant for noise properties. (#5082)
95-martin-orion Mar 31, 2022
c81d1e2
Update docs/transform.ipynb based on new transformer framework. (#5154)
tanujkhattar Apr 1, 2022
f3e225d
Enforce same control order in ControlledGate equality check (#5131)
daxfohl Apr 1, 2022
245e4cd
Fix gates.ipynb formatting (#5175)
dstrain115 Apr 1, 2022
2ff0110
Cleaning up cirq/install docs (#5178)
verult Apr 1, 2022
93376f9
Fix docs/circuits.ipynb as part of docs cleanup for Cirq 1.0 (#5177)
tanujkhattar Apr 1, 2022
768288f
Update simulator docs (#5182)
95-martin-orion Apr 1, 2022
c35cceb
Fix broken link from cirq/tutorials page as part of docs cleanup (#5174)
tanujkhattar Apr 2, 2022
bc324ec
Convert start.md to start.ipynb. (#5176)
MichaelBroughton Apr 2, 2022
d52a471
Update google concepts doc. (#5186)
dstrain115 Apr 4, 2022
0254bf6
Fix docs/qubits.ipynb as part of docs cleanup for Cirq 1.0 (#5179)
tanujkhattar Apr 4, 2022
a05e85b
Minor cleanup of QCS tutorial (#5184)
verult Apr 4, 2022
1d6ab17
Link fix on start page. (#5191)
MichaelBroughton Apr 5, 2022
4502833
Neutral atom update (#5192)
MichaelBroughton Apr 5, 2022
790b8b5
Remove use of deprecated device behavior from quantum_volume_errors. …
MichaelBroughton Apr 5, 2022
5d35706
Remove BooleanHamiltonian object (#5099)
tonybruguier Apr 5, 2022
492d6c6
Refactor qcs_notebook to use application default creds (#5045)
maffoo Apr 6, 2022
e6948b6
Cirq web supports LineQubits (#5211)
tonybruguier Apr 6, 2022
b93ebd0
Import new gapic generated code for quantum engine API (#5139)
maffoo Apr 6, 2022
327bb67
Fix basics (#5180)
MichaelBroughton Apr 7, 2022
da3a4a9
Deprecate `gate_set` parameter on engine classes. (#5207)
maffoo Apr 7, 2022
69a55dd
Tweak quantum walks doc (#5212)
dstrain115 Apr 7, 2022
203035a
Deprecate json_serializable_dataclass (#5208)
maffoo Apr 7, 2022
31e521c
Fix error and add test for mutable pauli string (#5213)
dabacon Apr 7, 2022
39443b0
Fix tutorials/state_histograms.ipynb as part of docs cleanup for Cirq…
tanujkhattar Apr 7, 2022
def1089
Fix tutorials/heatmaps.ipynb as part of docs cleanup for Cirq 1.0 (#5…
tanujkhattar Apr 7, 2022
0f5423d
Remove some 0.15 items from cirq.sim (#5137)
daxfohl Apr 7, 2022
06ca53b
Add Cynocracy to owners of cirq-ionq (#5145)
dabacon Apr 7, 2022
ecc3548
Fix educators/qaoa_ising.ipynb as part of docs cleanup for Cirq 1.0 (…
tanujkhattar Apr 7, 2022
10ab366
Improve documentation about writing type annotations (#5218)
maffoo Apr 7, 2022
136cf02
customer_gates.ipynb - minor content cleanup (#5215)
verult Apr 7, 2022
4f10a79
Make _commutes_ consistent (#5217)
dstrain115 Apr 7, 2022
a9e53a4
Fix numpy annotations np.array -> np.ndarray (#5227)
maffoo Apr 8, 2022
529c1ef
Delete the target_tensor parameter that was deprecated in 0.15 (#5225)
daxfohl Apr 8, 2022
78caf84
Deprecate the ActOnArgs._on_* methods (#5224)
daxfohl Apr 8, 2022
956f963
Allow specifying initial state vector in DensityMatrixSimulator (#5223)
maffoo Apr 8, 2022
0ce3f63
Unquote example code in ArithmeticOperation (#5230)
maffoo Apr 8, 2022
28e6ffb
Document CircuitOp optimizer best practices (#5221)
95-martin-orion Apr 8, 2022
a001399
Remove the deprecated mutators in cirq/ops (#5201)
daxfohl Apr 8, 2022
8bb5571
Allow server-side warnings from IonQ (#5222)
Cynocracy Apr 8, 2022
9c8dd63
Fix educators/textbook_algorithms.ipynb as part of docs cleanup for C…
tanujkhattar Apr 8, 2022
85c2419
Fix qcvv/xeb_theory.ipynb as part of docs cleanup for Cirq 1.0 (#5202)
tanujkhattar Apr 8, 2022
dd6ef68
Cleaning up tutorials/variational_algorithm.ipynb (#5185)
verult Apr 8, 2022
df5546d
Document CIRCUIT_TYPE and hide other typevars/aliases in circuits.py …
maffoo Apr 8, 2022
acdcc38
Make check scripts run pytest in parallel (#5143)
dabacon Apr 8, 2022
6d8da8f
cirq-ionq: Retry non-standard cloudflare errors (#5237)
Cynocracy Apr 8, 2022
8e26157
Add q helper function for constructing common qubit types. (#5181)
maffoo Apr 9, 2022
bdb6693
Unpin sympy and ignore sympy type errors (#5226)
maffoo Apr 11, 2022
877bb4b
Add calibration-to-noise pipeline (#5187)
95-martin-orion Apr 11, 2022
e18996d
Remove device from circuits (#5189)
MichaelBroughton Apr 11, 2022
2066869
Bump tensorflow-docs version (#5250)
maffoo Apr 12, 2022
e4961f1
Fix tutorials/qaoa.ipynb as part of docs cleanup for Cirq 1.0 (#5200)
tanujkhattar Apr 12, 2022
dc4b691
Remove custom cirq_type (#5249)
dabacon Apr 12, 2022
d634671
Skip "magic trailing comma" when formatting with black (#5170)
maffoo Apr 13, 2022
52ea259
Remove deprecated log_of_measurement_results parameters (#5233)
daxfohl Apr 13, 2022
b31be13
Make the quantum state generic (#5255)
daxfohl Apr 13, 2022
42e2118
Migrate google/best_practices.md to google/best_practices.ipynb as pa…
tanujkhattar Apr 13, 2022
dd98a9f
Format cirq-google with skip-magic-trailing-comma (#5171)
maffoo Apr 13, 2022
6b5835f
Format according to new black rules (#5259)
dabacon Apr 14, 2022
b7ba180
Ignore skip-magic-trailing-comma formatting with git blame (#5257)
maffoo Apr 14, 2022
d118dd0
Use sorted instead of set for random.sample (#5248)
dabacon Apr 14, 2022
6962d61
Fix Docs: cirq/tutorials/educators/chemistry (#5251)
TimoEckstein Apr 16, 2022
0fe4167
Convenience methods for modifying GoogleNoiseProperties (#5188)
95-martin-orion Apr 18, 2022
548f2c7
Deprecate Gateset.accept_global_phase_op (#5239)
daxfohl Apr 18, 2022
fc4bc2a
Exclude TYPE_CHECKING from docs (#5261)
tonybruguier Apr 19, 2022
e778c84
Add unit test
tonybruguier Apr 19, 2022
53dbcfe
Merge branch 'master' into cirq_qaoa
tonybruguier Apr 19, 2022
890efd1
Merge branch 'master' into cirq_qaoa
CirqBot Apr 26, 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
24 changes: 23 additions & 1 deletion examples/examples_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=wrong-or-nonexistent-copyright-notice
import itertools

import networkx
import numpy as np
import pytest
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -93,7 +94,28 @@ def test_example_heatmaps():


def test_example_runs_qaoa():
examples.qaoa.main(repetitions=10, maxiter=5)
examples.qaoa.main(repetitions=10, maxiter=5, use_boolean_hamiltonian_gate=False)
examples.qaoa.main(repetitions=10, maxiter=5, use_boolean_hamiltonian_gate=True)


def test_example_qaoa_same_unitary():
n = 6
p = 2

qubits = cirq.LineQubit.range(n)

graph = networkx.random_regular_graph(3, n)

betas = np.random.uniform(-np.pi, np.pi, size=p)
gammas = np.random.uniform(-np.pi, np.pi, size=p)
circuits = [
examples.qaoa.qaoa_max_cut_circuit(
qubits, betas, gammas, graph, use_boolean_hamiltonian_gate
)
for use_boolean_hamiltonian_gate in [True, False]
]

assert cirq.allclose_up_to_global_phase(cirq.unitary(circuits[0]), cirq.unitary(circuits[1]))


def test_example_runs_quantum_teleportation():
Expand Down
33 changes: 22 additions & 11 deletions examples/qaoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import cirq


def main(repetitions=1000, maxiter=50):
def main(repetitions=10, maxiter=50, use_boolean_hamiltonian_gate=False):
# Set problem parameters
n = 6
p = 2
Expand All @@ -72,7 +72,7 @@ def main(repetitions=1000, maxiter=50):
# 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)
circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph, use_boolean_hamiltonian_gate)
print('Example QAOA circuit:')
print(circuit.to_text_diagram(transpose=True))

Expand All @@ -89,7 +89,7 @@ def f(x):
# Create circuit
betas = x[:p]
gammas = x[p:]
circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph)
circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph, use_boolean_hamiltonian_gate)
# Sample bitstrings from circuit
result = simulator.run(circuit, repetitions=repetitions)
bitstrings = result.measurements['m']
Expand Down Expand Up @@ -128,18 +128,29 @@ def rzz(rads):
return cirq.ZZPowGate(exponent=2 * rads / np.pi, global_shift=-0.5)


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 qaoa_max_cut_circuit(qubits, betas, gammas, graph): # Nodes should be integers
def qaoa_max_cut_unitary(
qubits, betas, gammas, graph, use_boolean_hamiltonian_gate
): # Nodes should be integers
if use_boolean_hamiltonian_gate:
booleans = [f"x{i} ^ x{j}" for i, j in sorted(graph.edges)]
param_names = [f"x{i}" for i in range(len(qubits))]
for beta, gamma in zip(betas, gammas):
yield cirq.BooleanHamiltonianGate(param_names, booleans, 2.0 * gamma).on(*qubits)
yield cirq.rx(2 * beta).on_each(*qubits)
else:
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 qaoa_max_cut_circuit(
qubits, betas, gammas, graph, use_boolean_hamiltonian_gate
): # 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),
qaoa_max_cut_unitary(qubits, betas, gammas, graph, use_boolean_hamiltonian_gate),
# Measure
cirq.measure(*qubits, key='m'),
)
Expand Down