diff --git a/README.md b/README.md index 48b565e..898ed5a 100644 --- a/README.md +++ b/README.md @@ -132,5 +132,8 @@ the maximum. An optional `"seed"` (a non-negative integer, default 6680) seeds t sampler, the only random step, so the same request always gets the same response. Response includes `"answer"` (the S side in Redux certificate format, e.g. `"{1,3}"`), `"cut_weight"`, -`"answer_bitstring"` (one character per node, `1` = in S), `"seed"` (the seed used), and `"qasm"`. Invalid input returns +`"answer_bitstring"` (one character per node, `1` = in S), `"seed"` (the seed used), `"gammas"` and +`"betas"` (the optimized cost- and mixer-layer angles, one per layer), and `"qasm"`. The circuit is +written in `h`, `cx`, `rz` and `rx` gates: a Hadamard layer, then per layer a `cx`·`rz`·`cx` for each +edge and an `rx` on each node. Invalid input returns `{"error": ...}` with no `"answer"`. diff --git a/src/quantumsolver/maxcut_quantum.py b/src/quantumsolver/maxcut_quantum.py index 306c8f4..d572231 100644 --- a/src/quantumsolver/maxcut_quantum.py +++ b/src/quantumsolver/maxcut_quantum.py @@ -18,7 +18,9 @@ 'cut_weight': total weight of the cut, 'answer_bitstring': one character per node, '1' = in S, 'seed': the seed used, - 'qasm': the optimized QAOA circuit} + 'gammas': the optimized cost-layer angles, one per layer, + 'betas': the optimized mixer-layer angles, one per layer, + 'qasm': the optimized QAOA circuit, in h/cx/rz/rx gates} or {'error': message} for invalid input. """ @@ -27,8 +29,8 @@ import numpy as np import requests -from qiskit import qasm2, transpile -from qiskit.circuit.library import qaoa_ansatz +from qiskit import QuantumCircuit, qasm2 +from qiskit.circuit import ParameterVector from qiskit.primitives import StatevectorEstimator, StatevectorSampler from qiskit.quantum_info import SparsePauliOp from scipy.optimize import minimize @@ -41,7 +43,6 @@ # The estimator is exact and COBYLA is deterministic, so sampling is the only # random step; a fixed default seed makes every response reproducible. SEED = 6680 -BASIS_GATES = ["rz", "sx", "x", "cx"] def parse_graph(data): @@ -90,6 +91,52 @@ def cost_hamiltonian(n, weights): return SparsePauliOp.from_sparse_list(terms, num_qubits=n) +def edge_rounds(weights): + """Split the edges into rounds in which no two edges share a node (a greedy + edge colouring), as [[(i, j, w), ...], ...]. Gates on one round's edges + touch disjoint qubits, so they can run, and be drawn, side by side.""" + + rounds = [] + for (i, j), w in sorted(weights.items()): + for r in rounds: + if not r["used"] & {i, j}: + r["edges"].append((i, j, w)) + r["used"] |= {i, j} + break + else: + rounds.append({"edges": [(i, j, w)], "used": {i, j}}) + return [r["edges"] for r in rounds] + + +def qaoa_circuit(n, weights, reps): + """The QAOA ansatz for cost_hamiltonian(n, weights), built from h, cx, rz + and rx so its QASM reads as the textbook circuit. + + Each layer k applies the cost unitary exp(-i gamma_k H), whose term + (w / 2s) Z_i Z_j is RZZ(gamma_k w / s) = cx(i, j) rz(gamma_k w / s, j) + cx(i, j), then the mixer exp(-i beta_k sum X) = rx(2 beta_k) on every + qubit. This is the same state qaoa_ansatz builds, up to a global phase, + and the parameters keep its names so they sort the same way: + [beta[0..reps-1], gamma[0..reps-1]]. + """ + + scale = max(abs(w) for w in weights.values()) + beta = ParameterVector("β", reps) + gamma = ParameterVector("γ", reps) + circuit = QuantumCircuit(n) + circuit.h(range(n)) + for k in range(reps): + for edges in edge_rounds(weights): + for i, j, _ in edges: + circuit.cx(i, j) + for _, j, w in edges: + circuit.rz(gamma[k] * w / scale, j) + for i, j, _ in edges: + circuit.cx(i, j) + circuit.rx(2 * beta[k], range(n)) + return circuit + + def best_partition(counts, weights, n): """Pick the sampled bitstring with the largest cut, breaking ties by frequency. Partitions with S or T empty are skipped since Redux requires @@ -136,17 +183,12 @@ def solve(data) -> dict: circuit = None else: hamiltonian = cost_hamiltonian(n, weights) - # Lower the PauliEvolutionGates to standard gates once, up front. Left - # as-is, the simulator exponentiates a sparse matrix for every gate on - # every COBYLA step, which is orders of magnitude slower. - ansatz = transpile( - qaoa_ansatz(hamiltonian, reps=REPS), - basis_gates=BASIS_GATES, - optimization_level=1, - ) + # Built from standard gates, so the simulator runs it directly (no + # PauliEvolutionGates to exponentiate on every COBYLA step). + ansatz = qaoa_circuit(n, weights, REPS) # Same warm start as the notebook: midpoints of the beta and gamma - # periods. qaoa_ansatz orders its parameters [betas..., gammas...]. + # periods. The parameters sort as [betas..., gammas...]. init_params = [np.pi / 2] * REPS + [np.pi] * REPS estimator = StatevectorEstimator() @@ -180,6 +222,8 @@ def cost(params): "seed": seed, } if circuit is not None: + r["betas"] = [float(b) for b in result.x[:REPS]] + r["gammas"] = [float(g) for g in result.x[REPS:]] r["qasm"] = qasm2.dumps(circuit) return r diff --git a/tests/test_maxcut.py b/tests/test_maxcut.py index 82a0ebd..0abfda1 100644 --- a/tests/test_maxcut.py +++ b/tests/test_maxcut.py @@ -1,6 +1,11 @@ """Tests for the Max-Cut quantum solver.""" +import re + +import numpy as np import pytest +from qiskit.circuit.library import qaoa_ansatz +from qiskit.quantum_info import Statevector from quantumsolver import maxcut_quantum from quantumsolver.app import app as flask_app @@ -85,6 +90,47 @@ def test_custom_seed(): assert maxcut_quantum.solve({**REDUX_DEFAULT, "seed": 1}) == result +@pytest.mark.parametrize("data", [REDUX_DEFAULT, NOTEBOOK_GRAPH, TEN_NODES]) +def test_qaoa_circuit_matches_qaoa_ansatz(data): + """The hand-built circuit prepares the same state as qiskit's qaoa_ansatz + (up to global phase) and orders its parameters the same way.""" + nodes, weights = maxcut_quantum.parse_graph(data) + n = len(nodes) + ours = maxcut_quantum.qaoa_circuit(n, weights, maxcut_quantum.REPS) + reference = qaoa_ansatz( + maxcut_quantum.cost_hamiltonian(n, weights), reps=maxcut_quantum.REPS + ) + assert [p.name for p in ours.parameters] == [p.name for p in reference.parameters] + rng = np.random.default_rng(0) + for _ in range(3): + params = rng.uniform(-np.pi, np.pi, ours.num_parameters) + a = Statevector(ours.assign_parameters(params)).data + b = Statevector(reference.assign_parameters(params)).data + assert abs(np.vdot(a, b)) == pytest.approx(1.0) + + +def test_edge_rounds_are_matchings_covering_every_edge(): + """Each round's edges share no node, and every edge is in exactly one round.""" + _, weights = maxcut_quantum.parse_graph(TEN_NODES) + rounds = maxcut_quantum.edge_rounds(weights) + for edges in rounds: + touched = [q for i, j, _ in edges for q in (i, j)] + assert len(touched) == len(set(touched)) + assert sorted((i, j) for edges in rounds for i, j, _ in edges) == sorted(weights) + + +def test_response_has_angles_and_textbook_gates(): + """The response carries one gamma and one beta per layer, and the qasm uses + only h, cx, rz, rx and measurements.""" + result = maxcut_quantum.solve(REDUX_DEFAULT) + assert len(result["gammas"]) == maxcut_quantum.REPS + assert len(result["betas"]) == maxcut_quantum.REPS + gates = set(re.findall(r"^([a-z]+)[ (]", result["qasm"], re.MULTILINE)) + gates -= {"include", "qreg", "creg"} + assert gates <= {"h", "cx", "rz", "rx", "barrier", "measure"} + assert {"h", "cx", "rz", "rx", "measure"} <= gates + + def test_fractional_and_large_weights(): """Weight scale doesn't matter: the same graph with weights scaled up or to fractions gives the same partition quality."""