|
1 | 1 | import pytest |
2 | 2 | import numpy as np |
| 3 | +import networkx as nx |
3 | 4 | import collections |
| 5 | +import jijmodeling as jm |
| 6 | +import jijmodeling_transpiler.core as jmt |
4 | 7 | import quri_parts.circuit as qp_c |
5 | 8 | import quri_parts.core.operator as qp_o |
| 9 | +from quri_parts.qulacs.sampler import create_qulacs_vector_sampler |
6 | 10 | import qamomile.core.circuit as qm_c |
7 | 11 | import qamomile.core.operator as qm_o |
8 | 12 | import qamomile.core.bitssample as qm_bs |
| 13 | +import qamomile.core as qm |
9 | 14 | from qamomile.core.converters.qaoa import QAOAConverter |
10 | 15 | from qamomile.quri_parts.transpiler import QuriPartsTranspiler |
11 | 16 |
|
12 | | - |
| 17 | +def graph_coloring_problem() -> jm.Problem: |
| 18 | + # define variables |
| 19 | + V = jm.Placeholder("V") |
| 20 | + E = jm.Placeholder("E", ndim=2) |
| 21 | + N = jm.Placeholder("N") |
| 22 | + x = jm.BinaryVar("x", shape=(V, N)) |
| 23 | + n = jm.Element("i", belong_to=(0, N)) |
| 24 | + v = jm.Element("v", belong_to=(0, V)) |
| 25 | + e = jm.Element("e", belong_to=E) |
| 26 | + # set problem |
| 27 | + problem = jm.Problem("Graph Coloring") |
| 28 | + # set one-hot constraint that each vertex has only one color |
| 29 | + |
| 30 | + #problem += jm.Constraint("one-color", x[v, :].sum() == 1, forall=v) |
| 31 | + problem += jm.Constraint("one-color", jm.sum(n, x[v, n]) == 1, forall=v) |
| 32 | + # set objective function: minimize edges whose vertices connected by edges are the same color |
| 33 | + problem += jm.sum([n, e], x[e[0], n] * x[e[1], n]) |
| 34 | + return problem |
| 35 | + |
| 36 | +def graph_coloring_instance(): |
| 37 | + G = nx.Graph() |
| 38 | + G.add_nodes_from([0, 1, 2, 3]) |
| 39 | + G.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 3)]) |
| 40 | + E = [list(edge) for edge in G.edges] |
| 41 | + num_color = 3 |
| 42 | + num_nodes = G.number_of_nodes() |
| 43 | + instance_data = {"V": num_nodes, "N": num_color, "E": E} |
| 44 | + return instance_data |
| 45 | + |
| 46 | +def create_graph_coloring_operator_ansatz_initial_state( |
| 47 | + compiled_instance: jmt.CompiledInstance, num_nodes: int, num_color: int |
| 48 | +): |
| 49 | + n = num_color * num_nodes |
| 50 | + qc = qm_c.QuantumCircuit(n) |
| 51 | + var_map = compiled_instance.var_map.var_map["x"] |
| 52 | + for i in range(num_nodes): |
| 53 | + qc.x(var_map[(i, 0)]) # set all nodes to color 0 |
| 54 | + return qc |
13 | 55 |
|
14 | 56 | @pytest.fixture |
15 | 57 | def transpiler(): |
@@ -133,3 +175,20 @@ def test_qaoa_circuit(): |
133 | 175 | assert isinstance(qp_circuit, qp_c.LinearMappedUnboundParametricQuantumCircuit) |
134 | 176 | assert qp_circuit.qubit_count == 3 |
135 | 177 | assert qp_circuit.parameter_count == 4 |
| 178 | + |
| 179 | +def test_coloring_sample_decode(): |
| 180 | + problem = graph_coloring_problem() |
| 181 | + instance_data = graph_coloring_instance() |
| 182 | + compiled_instance = jmt.compile_model(problem, instance_data) |
| 183 | + initial_circuit = create_graph_coloring_operator_ansatz_initial_state(compiled_instance, instance_data['V'], instance_data['N']) |
| 184 | + qaoa_converter = qm.qaoa.QAOAConverter(compiled_instance) |
| 185 | + qaoa_converter.ising_encode(multipliers={"one-color": 1}) |
| 186 | + |
| 187 | + qp_transpiler = QuriPartsTranspiler() |
| 188 | + sampler = create_qulacs_vector_sampler() |
| 189 | + qp_circ = qp_transpiler.transpile_circuit(initial_circuit) |
| 190 | + qp_result = sampler(qp_circ, 10) |
| 191 | + |
| 192 | + sampleset = qaoa_converter.decode(qp_transpiler, (qp_result, initial_circuit.num_qubits)) |
| 193 | + assert sampleset[0].var_values["x"].values == {(0, 0): 1, (1, 0): 1, (2, 0): 1, (3, 0): 1} |
| 194 | + assert sampleset[0].num_occurrences == 10 |
0 commit comments