Skip to content

Commit

Permalink
(!) Update qaoa convention, finish migration to qiskit 1.0 in noteboo…
Browse files Browse the repository at this point in the history
…ks (#18)
  • Loading branch information
ElePT authored Feb 21, 2024
1 parent 87548b5 commit 329a82e
Show file tree
Hide file tree
Showing 9 changed files with 610 additions and 433 deletions.
444 changes: 270 additions & 174 deletions how_tos/how_to_build_qaoa_swap_circuit.ipynb

Large diffs are not rendered by default.

87 changes: 36 additions & 51 deletions how_tos/how_to_optimize_qaoa_for_hw.ipynb

Large diffs are not rendered by default.

294 changes: 167 additions & 127 deletions how_tos/how_to_run_custom_cost_functions.ipynb

Large diffs are not rendered by default.

145 changes: 87 additions & 58 deletions how_tos/how_to_select_qubits.ipynb

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion qopt_best_practices/qubit_selection/backend_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ class BackendEvaluator:

def __init__(self, backend: Backend):
self.backend = backend
self.coupling_map = CouplingMap(backend.coupling_map)
if backend.version == 2:
coupling_map = CouplingMap(backend.coupling_map)
else:
coupling_map = CouplingMap(backend.configuration().coupling_map)
self.coupling_map = coupling_map
if not self.coupling_map.is_symmetric:
self.coupling_map.make_symmetric()

Expand Down
38 changes: 26 additions & 12 deletions qopt_best_practices/qubit_selection/metric_evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,40 @@ def evaluate_fidelity(path: list[int], backend: Backend, edges: rx.EdgeList) ->
"""

two_qubit_fidelity = {}
target = backend.target

try:
gate_name = list(set(TWO_Q_GATES).intersection(backend.operation_names))[0]
except IndexError as exc:
raise ValueError("Could not identify two-qubit gate") from exc

for edge in edges:
if backend.version == 2:
target = backend.target
try:
gate_name = list(set(TWO_Q_GATES).intersection(backend.operation_names))[0]
except IndexError as exc:
raise ValueError("Could not identify two-qubit gate") from exc

for edge in edges:
try:
cx_error = target[gate_name][edge].error
except: # pylint: disable=bare-except
cx_error = target[gate_name][edge[::-1]].error

two_qubit_fidelity[tuple(edge)] = 1 - cx_error
else:
props = backend.properties()
try:
cx_error = target[gate_name][edge].error
except: # pylint: disable=bare-except
cx_error = target[gate_name][edge[::-1]].error
gate_name = list(set(TWO_Q_GATES).intersection(backend.configuration().basis_gates))[0]
except IndexError as exc:
raise ValueError("Could not identify two-qubit gate") from exc

two_qubit_fidelity[tuple(edge)] = 1 - cx_error
for edge in edges:
try:
cx_error = props.gate_error(gate_name, edge)
except: # pylint: disable=bare-except
cx_error = props.gate_error(gate_name, edge[::-1])

two_qubit_fidelity[tuple(edge)] = 1 - cx_error

if not path or len(path) == 1:
return 0.0

fidelity = 1.0
for idx in range(len(path) - 1):
fidelity *= two_qubit_fidelity[(path[idx], path[idx + 1])]

return fidelity
6 changes: 5 additions & 1 deletion qopt_best_practices/qubit_selection/qubit_subset_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ def find_lines(length: int, backend: Backend) -> list[int]:
The found paths.
"""

coupling_map = CouplingMap(backend.coupling_map)
if backend.version == 2:
coupling_map = CouplingMap(backend.coupling_map)
else:
coupling_map = CouplingMap(backend.configuration().coupling_map)

if not coupling_map.is_symmetric:
coupling_map.make_symmetric()

Expand Down
2 changes: 1 addition & 1 deletion qopt_best_practices/swap_strategies/build_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def apply_qaoa_layers( # pylint: disable=too-many-arguments,too-many-locals
mixer_layer = mixer
else:
mixer_layer = QuantumCircuit(num_qubits)
mixer_layer.rx(beta[0], range(num_qubits))
mixer_layer.rx(-2 * beta[0], range(num_qubits))

for layer in range(num_layers):
bind_dict = {cost_layer.parameters[0]: gamma[layer]}
Expand Down
21 changes: 13 additions & 8 deletions test/test_qubit_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
from unittest import TestCase

from qiskit_ibm_runtime.fake_provider import FakeSherbrooke
from qiskit_ibm_runtime.fake_provider import FakeSherbrooke, FakeHanoi, FakeHanoiV2
from qiskit.providers.fake_provider import GenericBackendV2


Expand All @@ -20,26 +20,24 @@ def setUp(self):

# load data
graph_file = os.path.join(os.path.dirname(__file__), "data/graph_2layers_0seed.json")

with open(graph_file, "r") as file:
data = json.load(file)

# 10 qubit graph
self.mapped_paulis = [tuple(pauli) for pauli in data["paulis"]]
self.mapped_graph = build_max_cut_graph(self.mapped_paulis)
self.backend = FakeSherbrooke()

def test_find_lines(self):
"""Test backend evaluation"""

paths = find_lines(len(self.mapped_graph), self.backend)

self.assertEqual(len(paths), 1336)
self.assertEqual(len(paths[0]), len(self.mapped_graph))
self.assertIsInstance(paths[0][0], int)

def test_find_lines_directed(self):
"Test backend with directed (asymmetric) coupling map"

"""Test backend with directed (asymmetric) coupling map"""
directed_fake_backend = GenericBackendV2(4, coupling_map=[[0, 1], [1, 2], [3, 2], [3, 0]])
lines = find_lines(3, backend=directed_fake_backend)

Expand All @@ -48,11 +46,18 @@ def test_find_lines_directed(self):

def test_qubit_selection(self):
"""Test backend evaluation"""

path_finder = BackendEvaluator(self.backend)

path, _, _ = path_finder.evaluate(len(self.mapped_graph))

expected_path = [45, 46, 47, 48, 49, 55, 68, 69, 70, 74]

self.assertEqual(set(path), set(expected_path))

def test_qubit_selection_v1_v2(self):
"""Test backend evaluation for 10 qubit line"""
backends = [FakeHanoi(), FakeHanoiV2()]

for backend in backends:
path_finder = BackendEvaluator(backend)
path, _, _ = path_finder.evaluate(len(self.mapped_graph))
expected_path = [1, 2, 4, 7, 8, 10, 11, 12, 13, 14]
self.assertEqual(set(path), set(expected_path))

0 comments on commit 329a82e

Please sign in to comment.