Skip to content

Commit

Permalink
Refactor, style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ElePT committed Nov 27, 2023
1 parent bf14ec4 commit 55f5407
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 18 deletions.
46 changes: 45 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,51 @@ variable-rgx = "[a-z_][a-z0-9_]{1,30}$"
max-line-length = 105 # default 100

[tool.pylint."messages control"]
disable = []
disable = [
# intentionally disabled:
"spelling", # too noisy
"fixme", # disabled as TODOs would show up as warnings
"protected-access", # disabled as we don't follow the public vs private convention strictly
"duplicate-code", # disabled as it is too verbose
"redundant-returns-doc", # for @abstractmethod, it cannot interpret "pass"
"too-many-lines", "too-many-branches", "too-many-locals", "too-many-nested-blocks", "too-many-statements",
"too-many-instance-attributes", "too-many-arguments", "too-many-public-methods", "too-few-public-methods", "too-many-ancestors",
"unnecessary-pass", # allow for methods with just "pass", for clarity
"no-else-return", # relax "elif" after a clause with a return
"docstring-first-line-empty", # relax docstring style
"import-outside-toplevel", "import-error", # overzealous with our optionals/dynamic packages
# TODO(#9614): these were added in modern Pylint. Decide if we want to enable them. If so,
# remove from here and fix the issues. Else, move it above this section and add a comment
# with the rationale
"arguments-renamed",
"broad-exception-raised",
"consider-iterating-dictionary",
"consider-using-dict-items",
"consider-using-enumerate",
"consider-using-f-string",
"modified-iterating-list",
"nested-min-max",
"no-member",
"no-value-for-parameter",
"non-ascii-name",
"not-context-manager",
"superfluous-parens",
"unknown-option-value",
"unexpected-keyword-arg",
"unnecessary-dict-index-lookup",
"unnecessary-direct-lambda-call",
"unnecessary-dunder-call",
"unnecessary-ellipsis",
"unnecessary-lambda-assignment",
"unnecessary-list-index-lookup",
"unspecified-encoding",
"unsupported-assignment-operation",
"use-dict-literal",
"use-list-literal",
"use-implicit-booleaness-not-comparison",
"use-maxsplit-arg",
]


enable = [
"use-symbolic-message-instead"
Expand Down
2 changes: 1 addition & 1 deletion qopt_best_practices/cost_function/cost_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def evaluate_sparse_pauli(state: int, observable: SparsePauliOp) -> complex:


def qaoa_sampler_cost_fun(params, ansatz, hamiltonian, sampler):

"""Standard sampler-based QAOA cost function to be plugged into optimizer routines."""
job = sampler.run(ansatz, params)
sampler_result = job.result()
sampled = sampler_result.quasi_dists[0]
Expand Down
11 changes: 6 additions & 5 deletions qopt_best_practices/swap_strategies/build_circuit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Circuit utils"""

from qiskit.circuit import QuantumCircuit, ClassicalRegister
from __future__ import annotations

from qiskit.circuit import QuantumCircuit
from qiskit.quantum_info import SparsePauliOp

from qiskit.transpiler import PassManager
Expand Down Expand Up @@ -53,7 +55,7 @@ def apply_swap_strategy(
return pm_pre.run(circuit)


def apply_qaoa_layers(
def apply_qaoa_layers( # pylint: disable=too-many-arguments,too-many-locals
cost_layer: QuantumCircuit,
meas_map: dict,
num_layers: int,
Expand All @@ -62,7 +64,7 @@ def apply_qaoa_layers(
initial_state: QuantumCircuit = None,
mixer: QuantumCircuit = None,
):
"""Construct the QAOA circuit.
"""Applies QAOA layers to construct circuit.
First, the initial state is applied. If `initial_state` is None we begin in the
initial superposition state. Next, we alternate between layers of the cot operator
Expand Down Expand Up @@ -114,7 +116,7 @@ def apply_qaoa_layers(
return new_circuit


def create_qaoa_swap_circuit(
def create_qaoa_swap_circuit( # pylint: disable=too-many-arguments
cost_operator: SparsePauliOp,
swap_strategy: SwapStrategy,
edge_coloring: dict = None,
Expand Down Expand Up @@ -151,7 +153,6 @@ def create_qaoa_swap_circuit(
qaoa_layers = len(theta) // 2
else:
gamma = beta = None
qaoa_layers = qaoa_layers

# First, create the ansatz of 1 layer of QAOA without mixer
cost_layer = QAOAAnsatz(
Expand Down
Empty file added run/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/test_cost_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for Cost Function Utils"""
10 changes: 6 additions & 4 deletions test/test_graph_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Tests for Graph Utils"""

from unittest import TestCase
import networkx as nx

Expand All @@ -8,13 +10,13 @@ class TestGraphRoundTrip(TestCase):
"""Test that we can convert between graph and Paulis."""

@staticmethod
def _test_edge_equality(g: nx.Graph, h: nx.Graph):
def _test_edge_equality(graph_1: nx.Graph, graph_2: nx.Graph):
"""Test equality of edges."""
if len(g.edges) != len(h.edges):
if len(graph_1.edges) != len(graph_2.edges):
return False

g_set = set(g.edges)
for edge in h.edges:
g_set = set(graph_1.edges)
for edge in graph_2.edges:
if edge not in g_set and edge[::-1] not in g_set:
return False

Expand Down
3 changes: 2 additions & 1 deletion test/test_qubit_selection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for Qubit Selection Utils"""

from unittest import TestCase
import json

from qiskit.providers.fake_provider import FakeWashington

from qopt_best_practices.utils import build_graph, build_paulis
Expand Down
2 changes: 2 additions & 0 deletions test/test_sat_mapping.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Tests for SAT Mapping Utils"""

from unittest import TestCase
import json

Expand Down
15 changes: 9 additions & 6 deletions test/test_swap_strategies.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Tests for Swap Strategies"""

from unittest import TestCase
import json

from qiskit.transpiler.passes.routing.commuting_2q_gate_routing import SwapStrategy
from qiskit.quantum_info import SparsePauliOp

from qopt_best_practices.swap_strategies import *
from qopt_best_practices.swap_strategies import create_qaoa_swap_circuit


class TestSwapStrategies(TestCase):
Expand All @@ -16,18 +18,19 @@ def setUp(self):
# load data
graph_file = "data/graph_2layers_0seed.json"

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

self.mapped_paulis = [tuple(pauli) for pauli in data["paulis"]]

self.hamiltonian = SparsePauliOp.from_list(self.mapped_paulis)

self.swap_strategy = SwapStrategy.from_line(
[i for i in range(len(self.original_graph.nodes))]
[i for i in range(self.hamiltonian.num_qubits)]
)

self.hamiltonian = SparsePauliOp.from_list(self.mapped_paulis)

def test_qaoa_circuit(self):
"""Test building the QAOA circuit."""

edge_coloring = {
(idx, idx + 1): (idx + 1) % 2 for idx in range(self.hamiltonian.num_qubits)
Expand Down

0 comments on commit 55f5407

Please sign in to comment.