From 7b29ecc264daf5d2447de56cc68bbd41555d0571 Mon Sep 17 00:00:00 2001 From: Yuki Sato <98375225+yksat@users.noreply.github.com> Date: Thu, 1 Aug 2024 16:05:01 +0900 Subject: [PATCH 1/5] Minor fixes --- qiskit_optimization/algorithms/qrao/magic_rounding.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qiskit_optimization/algorithms/qrao/magic_rounding.py b/qiskit_optimization/algorithms/qrao/magic_rounding.py index bc867a80..da976c5b 100644 --- a/qiskit_optimization/algorithms/qrao/magic_rounding.py +++ b/qiskit_optimization/algorithms/qrao/magic_rounding.py @@ -232,7 +232,8 @@ def _unpack_measurement_outcome( """ output_bits = [] # iterate in order over decision variables - for q, op in var2op.values(): + for var in sorted(var2op.keys()): + q, op = var2op[var] # get the decoding outcome index for the variable # corresponding to this Pauli op. op_index = self._OP_INDICES[vars_per_qubit][str(op.paulis[0])] From f9aaf8fe391325eb69a1c72216025fdcdbdf125c Mon Sep 17 00:00:00 2001 From: Yuki Sato <98375225+yksat@users.noreply.github.com> Date: Thu, 1 Aug 2024 16:06:27 +0900 Subject: [PATCH 2/5] Add files via upload --- test/algorithms/qrao/test_magic_rounding.py | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/algorithms/qrao/test_magic_rounding.py b/test/algorithms/qrao/test_magic_rounding.py index 15caae12..98bdf583 100644 --- a/test/algorithms/qrao/test_magic_rounding.py +++ b/test/algorithms/qrao/test_magic_rounding.py @@ -14,6 +14,7 @@ import unittest from test.optimization_test_case import QiskitOptimizationTestCase +import networkx as nx import numpy as np from qiskit.circuit import QuantumCircuit from qiskit.primitives import Sampler @@ -27,6 +28,7 @@ RoundingContext, RoundingResult, ) +from qiskit_optimization.applications import Maxcut from qiskit_optimization.problems import QuadraticProgram @@ -42,6 +44,15 @@ def setUp(self): self.problem.binary_var("z") self.problem.minimize(linear={"x": 1, "y": 2, "z": 3}) + # trivial maxcut problem (solution: {1}, {0, 2} with cut val 2) + graph = nx.Graph() + graph.add_nodes_from([0, 1, 2]) + graph.add_edges_from([(0, 1), (1, 2)]) + maxcut = Maxcut(graph) + self.maxcut_problem = maxcut.to_quadratic_program() + self.maxcut_optimal_value = 2.0 + self.maxcut_optimal_solution = [0, 1, 0] + def test_magic_rounding_constructor(self): """Test constructor""" sampler = Sampler(options={"shots": 10000, "seed": 42}) @@ -287,6 +298,19 @@ def test_magic_rounding_round_weighted_3_1_qrac(self): [0.2672612419124245, 0.5345224838248487, 0.8017837257372733], ) + def test_mapping_magic_rounding_result(self): + """Test the mapping of magic rounding result bits to variables""" + encoding = QuantumRandomAccessEncoding(max_vars_per_qubit=1) + encoding.encode(self.maxcut_problem) + circuit = encoding.state_preparation_circuit(self.maxcut_optimal_solution) + rounding_context = RoundingContext(encoding=encoding, expectation_values=0, circuit=circuit) + sampler = Sampler(options={"shots": 1}) + magic_rounding = MagicRounding(sampler=sampler) + rounding_result = magic_rounding.round(rounding_context) + solution = rounding_result.samples[0] + self.assertEqual(solution.fval, self.maxcut_optimal_value) + np.testing.assert_allclose(solution.x, self.maxcut_optimal_solution) + def test_magic_rounding_exceptions(self): """Test exceptions in the MagicRounding class""" encoding = QuantumRandomAccessEncoding(max_vars_per_qubit=3) From 591a203bef3db726b9c29c0177b71d0eadb3a33c Mon Sep 17 00:00:00 2001 From: Yuki Sato <98375225+yksat@users.noreply.github.com> Date: Thu, 1 Aug 2024 16:09:21 +0900 Subject: [PATCH 3/5] Add files via upload --- releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml diff --git a/releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml b/releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml new file mode 100644 index 00000000..4f4da8ba --- /dev/null +++ b/releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixed incorrect iteration order ``var2op.values()`` + in :class:`~qiskit_optimization.algorithms.qrao.MagicRounding`. \ No newline at end of file From 2ea65a82f3a8bd1c171093749bba6389f0e79e00 Mon Sep 17 00:00:00 2001 From: Yuki Sato <98375225+yksat@users.noreply.github.com> Date: Wed, 7 Aug 2024 10:32:34 +0900 Subject: [PATCH 4/5] simplify iterator expression Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com> --- qiskit_optimization/algorithms/qrao/magic_rounding.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/qiskit_optimization/algorithms/qrao/magic_rounding.py b/qiskit_optimization/algorithms/qrao/magic_rounding.py index da976c5b..4fc67ab2 100644 --- a/qiskit_optimization/algorithms/qrao/magic_rounding.py +++ b/qiskit_optimization/algorithms/qrao/magic_rounding.py @@ -232,8 +232,7 @@ def _unpack_measurement_outcome( """ output_bits = [] # iterate in order over decision variables - for var in sorted(var2op.keys()): - q, op = var2op[var] + for _, (q, op) in sorted(var2op.items()): # get the decoding outcome index for the variable # corresponding to this Pauli op. op_index = self._OP_INDICES[vars_per_qubit][str(op.paulis[0])] From c925d491cbfe8ea3874714fa0c08586b40f37afa Mon Sep 17 00:00:00 2001 From: Yuki Sato <98375225+yksat@users.noreply.github.com> Date: Wed, 7 Aug 2024 12:51:33 +0900 Subject: [PATCH 5/5] modify releasenotes Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com> --- releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml b/releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml index 4f4da8ba..ec10897d 100644 --- a/releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml +++ b/releasenotes/notes/fix-qrao-d0402da6a5c40121.yaml @@ -1,5 +1,4 @@ --- fixes: - | - Fixed incorrect iteration order ``var2op.values()`` - in :class:`~qiskit_optimization.algorithms.qrao.MagicRounding`. \ No newline at end of file + Fixed a bug of :meth:`.MagicRounding.round` that may return a wrong decision variable order. \ No newline at end of file