We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Deprecate gateset.accepts_global_phase
**Description of the issue**

Issue requested by @tanujkhattar 

xref: https://github.com/quantumlib/Cirq/pull/4697/files/d64eb23319c0eb7664526613b95db368659fb7aa#r766054614

Since global phase has a gate, it can be inspected like any other gate. Therefore gatesets no longer need special handling for global phase.
mypy check is failing on `CI` for sympy `v1.10`
**Description of the issue**
See: https://github.com/quantumlib/Cirq/pull/4936#issuecomment-1060953773

**Cirq version**
`master` version


Docs: Filter out TYPE_CHECKING from public docs
**Description of the issue**

The `TYPE_CHECKING` variable imported from `typing` shows up in API docs (example: https://github.com/quantumlib/Cirq/issues/5150). We should filter it out, since it's not part of the cirq API. Per @dabacon's [comment](https://github.com/quantumlib/Cirq/pull/5229#issuecomment-1093080151), we should be able to do this in `dev_tools/docs/build_api_docs.py`.

Deprecation of abstract/protocol methods
We need a better way to deprecate abstract methods.

Context (#3860): 

@balopat  This ends up being a breaking change though for any simulator that was overriding `_simulator_iterator`. This is why I had left the call to `_simulator_iterator` there in the original PR #3650. Otherwise I'd have just inlined and deleted `_simulator_iterator` outright: it's a private method and it's unused elsewhere.

It doesn't sound like anyone has been broken by this, so maybe it's okay. (We can just delete `_simulator_iterator` in that case; it's not called by anyone). I think there's still an unmet need for cleanly deprecate abstract methods, or methods that were intended to be overridden. The base class has to continue calling them until the deadline or it will break child classes. I like the error-by-default, but maybe there's a way to silence these errors when retaining the call to a deprecated method is required, like here.

_Originally posted by @daxfohl in https://github.com/quantumlib/Cirq/pull/3860#discussion_r587578684_

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `examples/qaoa.py`
Content:
```
1 # pylint: disable=wrong-or-nonexistent-copyright-notice
2 """Runs the Quantum Approximate Optimization Algorithm on Max-Cut.
3 
4 === EXAMPLE OUTPUT ===
5 
6 Example QAOA circuit:
7   0           1           2           3           4           5
8   │           │           │           │           │           │
9   H           H           H           H           H           H
10   │           │           │           │           │           │
11   ZZ──────────ZZ^(-4/13)  │           │           │           │
12 ┌ │           │           │           │           │           │           ┐
13 │ ZZ──────────┼───────────ZZ^(-4/13)  │           │           │           │
14 │ │           ZZ──────────┼───────────ZZ^(-4/13)  │           │           │
15 └ │           │           │           │           │           │           ┘
16 ┌ │           │           │           │           │           │           ┐
17 │ ZZ──────────┼───────────┼───────────┼───────────ZZ^(-4/13)  │           │
18 │ │           ZZ──────────┼───────────┼───────────┼───────────ZZ^(-4/13)  │
19 └ │           │           │           │           │           │           ┘
20   Rx(0.151π)  Rx(0.151π)  ZZ──────────┼───────────ZZ^(-4/13)  │
21   │           │           │           │           │           │
22   ZZ──────────ZZ^-0.941   ZZ──────────┼───────────┼───────────ZZ^(-4/13)
23   │           │           │           ZZ──────────ZZ^(-4/13)  │
24 ┌ │           │           │           │           │           │           ┐
25 │ │           │           Rx(0.151π)  ZZ──────────┼───────────ZZ^(-4/13)  │
26 │ │           │           │           │           Rx(0.151π)  │           │
27 └ │           │           │           │           │           │           ┘
28   ZZ──────────┼───────────ZZ^-0.941   Rx(0.151π)  │           Rx(0.151π)
29 ┌ │           │           │           │           │           │           ┐
30 │ ZZ──────────┼───────────┼───────────┼───────────ZZ^-0.941   │           │
31 │ │           ZZ──────────┼───────────ZZ^-0.941   │           │           │
32 └ │           │           │           │           │           │           ┘
33   Rx(-0.448π) ZZ──────────┼───────────┼───────────┼───────────ZZ^-0.941
34   │           │           ZZ──────────┼───────────ZZ^-0.941   │
35   │           │           │           │           │           │
36   │           Rx(-0.448π) ZZ──────────┼───────────┼───────────ZZ^-0.941
37   │           │           │           ZZ──────────ZZ^-0.941   │
38 ┌ │           │           │           │           │           │           ┐
39 │ │           │           Rx(-0.448π) ZZ──────────┼───────────ZZ^-0.941   │
40 │ │           │           │           │           Rx(-0.448π) │           │
41 └ │           │           │           │           │           │           ┘
42   │           │           │           Rx(-0.448π) │           Rx(-0.448π)
43   │           │           │           │           │           │
44   M('m')──────M───────────M───────────M───────────M───────────M
45   │           │           │           │           │           │
46 Optimizing objective function ...
47 The largest cut value found was 7.
48 The largest possible cut has size 7.
49 The approximation ratio achieved is 1.0.
50 """
51 
52 import itertools
53 
54 import numpy as np
55 import networkx
56 import scipy.optimize
57 
58 import cirq
59 
60 
61 def main(repetitions=1000, maxiter=50):
62     # Set problem parameters
63     n = 6
64     p = 2
65 
66     # Generate a random 3-regular graph on n nodes
67     graph = networkx.random_regular_graph(3, n)
68 
69     # Make qubits
70     qubits = cirq.LineQubit.range(n)
71 
72     # Print an example circuit
73     betas = np.random.uniform(-np.pi, np.pi, size=p)
74     gammas = np.random.uniform(-np.pi, np.pi, size=p)
75     circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph)
76     print('Example QAOA circuit:')
77     print(circuit.to_text_diagram(transpose=True))
78 
79     # Create variables to store the largest cut and cut value found
80     largest_cut_found = None
81     largest_cut_value_found = 0
82 
83     # Initialize simulator
84     simulator = cirq.Simulator()
85 
86     # Define objective function (we'll use the negative expected cut value)
87 
88     def f(x):
89         # Create circuit
90         betas = x[:p]
91         gammas = x[p:]
92         circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph)
93         # Sample bitstrings from circuit
94         result = simulator.run(circuit, repetitions=repetitions)
95         bitstrings = result.measurements['m']
96         # Process bitstrings
97         nonlocal largest_cut_found
98         nonlocal largest_cut_value_found
99         values = cut_values(bitstrings, graph)
100         max_value_index = np.argmax(values)
101         max_value = values[max_value_index]
102         if max_value > largest_cut_value_found:
103             largest_cut_value_found = max_value
104             largest_cut_found = bitstrings[max_value_index]
105         mean = np.mean(values)
106         return -mean
107 
108     # Pick an initial guess
109     x0 = np.random.uniform(-np.pi, np.pi, size=2 * p)
110 
111     # Optimize f
112     print('Optimizing objective function ...')
113     scipy.optimize.minimize(f, x0, method='Nelder-Mead', options={'maxiter': maxiter})
114 
115     # Compute best possible cut value via brute force search
116     all_bitstrings = np.array(list(itertools.product(range(2), repeat=n)))
117     all_values = cut_values(all_bitstrings, graph)
118     max_cut_value = np.max(all_values)
119 
120     # Print the results
121     print(f'The largest cut value found was {largest_cut_value_found}.')
122     print(f'The largest possible cut has size {max_cut_value}.')
123     print(f'The approximation ratio achieved is {largest_cut_value_found / max_cut_value}.')
124 
125 
126 def rzz(rads):
127     """Returns a gate with the matrix exp(-i Z⊗Z rads)."""
128     return cirq.ZZPowGate(exponent=2 * rads / np.pi, global_shift=-0.5)
129 
130 
131 def qaoa_max_cut_unitary(qubits, betas, gammas, graph):  # Nodes should be integers
132     for beta, gamma in zip(betas, gammas):
133         yield (rzz(-0.5 * gamma).on(qubits[i], qubits[j]) for i, j in graph.edges)
134         yield cirq.rx(2 * beta).on_each(*qubits)
135 
136 
137 def qaoa_max_cut_circuit(qubits, betas, gammas, graph):  # Nodes should be integers
138     return cirq.Circuit(
139         # Prepare uniform superposition
140         cirq.H.on_each(*qubits),
141         # Apply QAOA unitary
142         qaoa_max_cut_unitary(qubits, betas, gammas, graph),
143         # Measure
144         cirq.measure(*qubits, key='m'),
145     )
146 
147 
148 def cut_values(bitstrings, graph):
149     mat = networkx.adjacency_matrix(graph, nodelist=sorted(graph.nodes))
150     vecs = (-1) ** bitstrings
151     vals = 0.5 * np.sum(vecs * (mat @ vecs.T).T, axis=-1)
152     vals = 0.5 * (graph.size() - vals)
153     return vals
154 
155 
156 if __name__ == '__main__':
157     main()
158
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/examples/qaoa.py b/examples/qaoa.py
--- a/examples/qaoa.py
+++ b/examples/qaoa.py
@@ -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
@@ -72,7 +72,7 @@
     # 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))
 
@@ -89,7 +89,7 @@
         # 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']
@@ -128,18 +128,29 @@
     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'),
     )

{"golden_diff": "diff --git a/examples/qaoa.py b/examples/qaoa.py\n--- a/examples/qaoa.py\n+++ b/examples/qaoa.py\n@@ -58,7 +58,7 @@\n import cirq\n \n \n-def main(repetitions=1000, maxiter=50):\n+def main(repetitions=10, maxiter=50, use_boolean_hamiltonian_gate=False):\n     # Set problem parameters\n     n = 6\n     p = 2\n@@ -72,7 +72,7 @@\n     # Print an example circuit\n     betas = np.random.uniform(-np.pi, np.pi, size=p)\n     gammas = np.random.uniform(-np.pi, np.pi, size=p)\n-    circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph)\n+    circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph, use_boolean_hamiltonian_gate)\n     print('Example QAOA circuit:')\n     print(circuit.to_text_diagram(transpose=True))\n \n@@ -89,7 +89,7 @@\n         # Create circuit\n         betas = x[:p]\n         gammas = x[p:]\n-        circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph)\n+        circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph, use_boolean_hamiltonian_gate)\n         # Sample bitstrings from circuit\n         result = simulator.run(circuit, repetitions=repetitions)\n         bitstrings = result.measurements['m']\n@@ -128,18 +128,29 @@\n     return cirq.ZZPowGate(exponent=2 * rads / np.pi, global_shift=-0.5)\n \n \n-def qaoa_max_cut_unitary(qubits, betas, gammas, graph):  # Nodes should be integers\n-    for beta, gamma in zip(betas, gammas):\n-        yield (rzz(-0.5 * gamma).on(qubits[i], qubits[j]) for i, j in graph.edges)\n-        yield cirq.rx(2 * beta).on_each(*qubits)\n-\n-\n-def qaoa_max_cut_circuit(qubits, betas, gammas, graph):  # Nodes should be integers\n+def qaoa_max_cut_unitary(\n+    qubits, betas, gammas, graph, use_boolean_hamiltonian_gate\n+):  # Nodes should be integers\n+    if use_boolean_hamiltonian_gate:\n+        booleans = [f\"x{i} ^ x{j}\" for i, j in sorted(graph.edges)]\n+        param_names = [f\"x{i}\" for i in range(len(qubits))]\n+        for beta, gamma in zip(betas, gammas):\n+            yield cirq.BooleanHamiltonianGate(param_names, booleans, 2.0 * gamma).on(*qubits)\n+            yield cirq.rx(2 * beta).on_each(*qubits)\n+    else:\n+        for beta, gamma in zip(betas, gammas):\n+            yield (rzz(-0.5 * gamma).on(qubits[i], qubits[j]) for i, j in graph.edges)\n+            yield cirq.rx(2 * beta).on_each(*qubits)\n+\n+\n+def qaoa_max_cut_circuit(\n+    qubits, betas, gammas, graph, use_boolean_hamiltonian_gate\n+):  # Nodes should be integers\n     return cirq.Circuit(\n         # Prepare uniform superposition\n         cirq.H.on_each(*qubits),\n         # Apply QAOA unitary\n-        qaoa_max_cut_unitary(qubits, betas, gammas, graph),\n+        qaoa_max_cut_unitary(qubits, betas, gammas, graph, use_boolean_hamiltonian_gate),\n         # Measure\n         cirq.measure(*qubits, key='m'),\n     )\n", "issue": "Deprecate gateset.accepts_global_phase\n**Description of the issue**\r\n\r\nIssue requested by @tanujkhattar \r\n\r\nxref: https://github.com/quantumlib/Cirq/pull/4697/files/d64eb23319c0eb7664526613b95db368659fb7aa#r766054614\r\n\r\nSince global phase has a gate, it can be inspected like any other gate. Therefore gatesets no longer need special handling for global phase.\nmypy check is failing on `CI` for sympy `v1.10`\n**Description of the issue**\r\nSee: https://github.com/quantumlib/Cirq/pull/4936#issuecomment-1060953773\r\n\r\n**Cirq version**\r\n`master` version\r\n\r\n\nDocs: Filter out TYPE_CHECKING from public docs\n**Description of the issue**\r\n\r\nThe `TYPE_CHECKING` variable imported from `typing` shows up in API docs (example: https://github.com/quantumlib/Cirq/issues/5150). We should filter it out, since it's not part of the cirq API. Per @dabacon's [comment](https://github.com/quantumlib/Cirq/pull/5229#issuecomment-1093080151), we should be able to do this in `dev_tools/docs/build_api_docs.py`.\r\n\nDeprecation of abstract/protocol methods\nWe need a better way to deprecate abstract methods.\r\n\r\nContext (#3860): \r\n\r\n@balopat  This ends up being a breaking change though for any simulator that was overriding `_simulator_iterator`. This is why I had left the call to `_simulator_iterator` there in the original PR #3650. Otherwise I'd have just inlined and deleted `_simulator_iterator` outright: it's a private method and it's unused elsewhere.\r\n\r\nIt doesn't sound like anyone has been broken by this, so maybe it's okay. (We can just delete `_simulator_iterator` in that case; it's not called by anyone). I think there's still an unmet need for cleanly deprecate abstract methods, or methods that were intended to be overridden. The base class has to continue calling them until the deadline or it will break child classes. I like the error-by-default, but maybe there's a way to silence these errors when retaining the call to a deprecated method is required, like here.\r\n\r\n_Originally posted by @daxfohl in https://github.com/quantumlib/Cirq/pull/3860#discussion_r587578684_\n", "before_files": [{"content": "# pylint: disable=wrong-or-nonexistent-copyright-notice\n\"\"\"Runs the Quantum Approximate Optimization Algorithm on Max-Cut.\n\n=== EXAMPLE OUTPUT ===\n\nExample QAOA circuit:\n  0           1           2           3           4           5\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  H           H           H           H           H           H\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502           \u2502           \u2502           \u2502\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502           \u2502           \u2502           \u2502\n\u2502 \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502           \u2502           \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502           \u2502\n\u2502 \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n  Rx(0.151\u03c0)  Rx(0.151\u03c0)  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)\n  \u2502           \u2502           \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 \u2502           \u2502           Rx(0.151\u03c0)  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502\n\u2502 \u2502           \u2502           \u2502           \u2502           Rx(0.151\u03c0)  \u2502           \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   Rx(0.151\u03c0)  \u2502           Rx(0.151\u03c0)\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502           \u2502\n\u2502 \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502           \u2502           \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n  Rx(-0.448\u03c0) ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941\n  \u2502           \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  \u2502           Rx(-0.448\u03c0) ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941\n  \u2502           \u2502           \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 \u2502           \u2502           Rx(-0.448\u03c0) ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502\n\u2502 \u2502           \u2502           \u2502           \u2502           Rx(-0.448\u03c0) \u2502           \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n  \u2502           \u2502           \u2502           Rx(-0.448\u03c0) \u2502           Rx(-0.448\u03c0)\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  M('m')\u2500\u2500\u2500\u2500\u2500\u2500M\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500M\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500M\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500M\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500M\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\nOptimizing objective function ...\nThe largest cut value found was 7.\nThe largest possible cut has size 7.\nThe approximation ratio achieved is 1.0.\n\"\"\"\n\nimport itertools\n\nimport numpy as np\nimport networkx\nimport scipy.optimize\n\nimport cirq\n\n\ndef main(repetitions=1000, maxiter=50):\n    # Set problem parameters\n    n = 6\n    p = 2\n\n    # Generate a random 3-regular graph on n nodes\n    graph = networkx.random_regular_graph(3, n)\n\n    # Make qubits\n    qubits = cirq.LineQubit.range(n)\n\n    # Print an example circuit\n    betas = np.random.uniform(-np.pi, np.pi, size=p)\n    gammas = np.random.uniform(-np.pi, np.pi, size=p)\n    circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph)\n    print('Example QAOA circuit:')\n    print(circuit.to_text_diagram(transpose=True))\n\n    # Create variables to store the largest cut and cut value found\n    largest_cut_found = None\n    largest_cut_value_found = 0\n\n    # Initialize simulator\n    simulator = cirq.Simulator()\n\n    # Define objective function (we'll use the negative expected cut value)\n\n    def f(x):\n        # Create circuit\n        betas = x[:p]\n        gammas = x[p:]\n        circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph)\n        # Sample bitstrings from circuit\n        result = simulator.run(circuit, repetitions=repetitions)\n        bitstrings = result.measurements['m']\n        # Process bitstrings\n        nonlocal largest_cut_found\n        nonlocal largest_cut_value_found\n        values = cut_values(bitstrings, graph)\n        max_value_index = np.argmax(values)\n        max_value = values[max_value_index]\n        if max_value > largest_cut_value_found:\n            largest_cut_value_found = max_value\n            largest_cut_found = bitstrings[max_value_index]\n        mean = np.mean(values)\n        return -mean\n\n    # Pick an initial guess\n    x0 = np.random.uniform(-np.pi, np.pi, size=2 * p)\n\n    # Optimize f\n    print('Optimizing objective function ...')\n    scipy.optimize.minimize(f, x0, method='Nelder-Mead', options={'maxiter': maxiter})\n\n    # Compute best possible cut value via brute force search\n    all_bitstrings = np.array(list(itertools.product(range(2), repeat=n)))\n    all_values = cut_values(all_bitstrings, graph)\n    max_cut_value = np.max(all_values)\n\n    # Print the results\n    print(f'The largest cut value found was {largest_cut_value_found}.')\n    print(f'The largest possible cut has size {max_cut_value}.')\n    print(f'The approximation ratio achieved is {largest_cut_value_found / max_cut_value}.')\n\n\ndef rzz(rads):\n    \"\"\"Returns a gate with the matrix exp(-i Z\u2297Z rads).\"\"\"\n    return cirq.ZZPowGate(exponent=2 * rads / np.pi, global_shift=-0.5)\n\n\ndef qaoa_max_cut_unitary(qubits, betas, gammas, graph):  # Nodes should be integers\n    for beta, gamma in zip(betas, gammas):\n        yield (rzz(-0.5 * gamma).on(qubits[i], qubits[j]) for i, j in graph.edges)\n        yield cirq.rx(2 * beta).on_each(*qubits)\n\n\ndef qaoa_max_cut_circuit(qubits, betas, gammas, graph):  # Nodes should be integers\n    return cirq.Circuit(\n        # Prepare uniform superposition\n        cirq.H.on_each(*qubits),\n        # Apply QAOA unitary\n        qaoa_max_cut_unitary(qubits, betas, gammas, graph),\n        # Measure\n        cirq.measure(*qubits, key='m'),\n    )\n\n\ndef cut_values(bitstrings, graph):\n    mat = networkx.adjacency_matrix(graph, nodelist=sorted(graph.nodes))\n    vecs = (-1) ** bitstrings\n    vals = 0.5 * np.sum(vecs * (mat @ vecs.T).T, axis=-1)\n    vals = 0.5 * (graph.size() - vals)\n    return vals\n\n\nif __name__ == '__main__':\n    main()\n", "path": "examples/qaoa.py"}], "after_files": [{"content": "# pylint: disable=wrong-or-nonexistent-copyright-notice\n\"\"\"Runs the Quantum Approximate Optimization Algorithm on Max-Cut.\n\n=== EXAMPLE OUTPUT ===\n\nExample QAOA circuit:\n  0           1           2           3           4           5\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  H           H           H           H           H           H\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502           \u2502           \u2502           \u2502\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502           \u2502           \u2502           \u2502\n\u2502 \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502           \u2502           \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502           \u2502\n\u2502 \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n  Rx(0.151\u03c0)  Rx(0.151\u03c0)  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)\n  \u2502           \u2502           \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 \u2502           \u2502           Rx(0.151\u03c0)  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^(-4/13)  \u2502\n\u2502 \u2502           \u2502           \u2502           \u2502           Rx(0.151\u03c0)  \u2502           \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n  ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   Rx(0.151\u03c0)  \u2502           Rx(0.151\u03c0)\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502           \u2502\n\u2502 \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502           \u2502           \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n  Rx(-0.448\u03c0) ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941\n  \u2502           \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  \u2502           Rx(-0.448\u03c0) ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941\n  \u2502           \u2502           \u2502           ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502\n\u250c \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2510\n\u2502 \u2502           \u2502           Rx(-0.448\u03c0) ZZ\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500ZZ^-0.941   \u2502\n\u2502 \u2502           \u2502           \u2502           \u2502           Rx(-0.448\u03c0) \u2502           \u2502\n\u2514 \u2502           \u2502           \u2502           \u2502           \u2502           \u2502           \u2518\n  \u2502           \u2502           \u2502           Rx(-0.448\u03c0) \u2502           Rx(-0.448\u03c0)\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\n  M('m')\u2500\u2500\u2500\u2500\u2500\u2500M\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500M\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500M\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500M\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500M\n  \u2502           \u2502           \u2502           \u2502           \u2502           \u2502\nOptimizing objective function ...\nThe largest cut value found was 7.\nThe largest possible cut has size 7.\nThe approximation ratio achieved is 1.0.\n\"\"\"\n\nimport itertools\n\nimport numpy as np\nimport networkx\nimport scipy.optimize\n\nimport cirq\n\n\ndef main(repetitions=10, maxiter=50, use_boolean_hamiltonian_gate=False):\n    # Set problem parameters\n    n = 6\n    p = 2\n\n    # Generate a random 3-regular graph on n nodes\n    graph = networkx.random_regular_graph(3, n)\n\n    # Make qubits\n    qubits = cirq.LineQubit.range(n)\n\n    # Print an example circuit\n    betas = np.random.uniform(-np.pi, np.pi, size=p)\n    gammas = np.random.uniform(-np.pi, np.pi, size=p)\n    circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph, use_boolean_hamiltonian_gate)\n    print('Example QAOA circuit:')\n    print(circuit.to_text_diagram(transpose=True))\n\n    # Create variables to store the largest cut and cut value found\n    largest_cut_found = None\n    largest_cut_value_found = 0\n\n    # Initialize simulator\n    simulator = cirq.Simulator()\n\n    # Define objective function (we'll use the negative expected cut value)\n\n    def f(x):\n        # Create circuit\n        betas = x[:p]\n        gammas = x[p:]\n        circuit = qaoa_max_cut_circuit(qubits, betas, gammas, graph, use_boolean_hamiltonian_gate)\n        # Sample bitstrings from circuit\n        result = simulator.run(circuit, repetitions=repetitions)\n        bitstrings = result.measurements['m']\n        # Process bitstrings\n        nonlocal largest_cut_found\n        nonlocal largest_cut_value_found\n        values = cut_values(bitstrings, graph)\n        max_value_index = np.argmax(values)\n        max_value = values[max_value_index]\n        if max_value > largest_cut_value_found:\n            largest_cut_value_found = max_value\n            largest_cut_found = bitstrings[max_value_index]\n        mean = np.mean(values)\n        return -mean\n\n    # Pick an initial guess\n    x0 = np.random.uniform(-np.pi, np.pi, size=2 * p)\n\n    # Optimize f\n    print('Optimizing objective function ...')\n    scipy.optimize.minimize(f, x0, method='Nelder-Mead', options={'maxiter': maxiter})\n\n    # Compute best possible cut value via brute force search\n    all_bitstrings = np.array(list(itertools.product(range(2), repeat=n)))\n    all_values = cut_values(all_bitstrings, graph)\n    max_cut_value = np.max(all_values)\n\n    # Print the results\n    print(f'The largest cut value found was {largest_cut_value_found}.')\n    print(f'The largest possible cut has size {max_cut_value}.')\n    print(f'The approximation ratio achieved is {largest_cut_value_found / max_cut_value}.')\n\n\ndef rzz(rads):\n    \"\"\"Returns a gate with the matrix exp(-i Z\u2297Z rads).\"\"\"\n    return cirq.ZZPowGate(exponent=2 * rads / np.pi, global_shift=-0.5)\n\n\ndef qaoa_max_cut_unitary(\n    qubits, betas, gammas, graph, use_boolean_hamiltonian_gate\n):  # Nodes should be integers\n    if use_boolean_hamiltonian_gate:\n        booleans = [f\"x{i} ^ x{j}\" for i, j in sorted(graph.edges)]\n        param_names = [f\"x{i}\" for i in range(len(qubits))]\n        for beta, gamma in zip(betas, gammas):\n            yield cirq.BooleanHamiltonianGate(param_names, booleans, 2.0 * gamma).on(*qubits)\n            yield cirq.rx(2 * beta).on_each(*qubits)\n    else:\n        for beta, gamma in zip(betas, gammas):\n            yield (rzz(-0.5 * gamma).on(qubits[i], qubits[j]) for i, j in graph.edges)\n            yield cirq.rx(2 * beta).on_each(*qubits)\n\n\ndef qaoa_max_cut_circuit(\n    qubits, betas, gammas, graph, use_boolean_hamiltonian_gate\n):  # Nodes should be integers\n    return cirq.Circuit(\n        # Prepare uniform superposition\n        cirq.H.on_each(*qubits),\n        # Apply QAOA unitary\n        qaoa_max_cut_unitary(qubits, betas, gammas, graph, use_boolean_hamiltonian_gate),\n        # Measure\n        cirq.measure(*qubits, key='m'),\n    )\n\n\ndef cut_values(bitstrings, graph):\n    mat = networkx.adjacency_matrix(graph, nodelist=sorted(graph.nodes))\n    vecs = (-1) ** bitstrings\n    vals = 0.5 * np.sum(vecs * (mat @ vecs.T).T, axis=-1)\n    vals = 0.5 * (graph.size() - vals)\n    return vals\n\n\nif __name__ == '__main__':\n    main()\n", "path": "examples/qaoa.py"}]}