Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Enable pylint too-many-locals check
Browse files Browse the repository at this point in the history
Partially addresses #16
  • Loading branch information
garrison committed May 27, 2022
1 parent d080bd6 commit eb9c418
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
64 changes: 37 additions & 27 deletions qrao/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,41 @@ def term2op(self, *variables: int) -> PauliOp:
done.add(pos)
return reduce(lambda x, y: x ^ y, ops)

@staticmethod
def _generate_ising_terms(
problem: QuadraticProgram,
) -> Tuple[float, np.ndarray, np.ndarray]:
num_vars = problem.get_num_vars()

# set a sign corresponding to a maximized or minimized problem:
# 1 is for minimized problem, -1 is for maximized problem.
sense = problem.objective.sense.value

# convert a constant part of the objective function into Hamiltonian.
offset = problem.objective.constant * sense

# convert linear parts of the objective function into Hamiltonian.
linear = np.zeros(num_vars)
for idx, coef in problem.objective.linear.to_dict().items():
weight = coef * sense / 2
linear[idx] -= weight
offset += weight

# convert quadratic parts of the objective function into Hamiltonian.
quad = np.zeros((num_vars, num_vars))
for (i, j), coef in problem.objective.quadratic.to_dict().items():
weight = coef * sense / 4
if i == j:
linear[i] -= 2 * weight
offset += 2 * weight
else:
quad[i, j] += weight
linear[i] -= weight
linear[j] -= weight
offset += weight

return offset, linear, quad

@staticmethod
def _find_variable_partition(quad: np.ndarray) -> Dict[int, List[int]]:
num_nodes = quad.shape[0]
Expand Down Expand Up @@ -459,35 +494,10 @@ def encode(self, problem: QuadraticProgram) -> None:
"constraints to penalty terms of the objective function."
)

# initialize Hamiltonian.
num_vars = problem.get_num_vars()

# set a sign corresponding to a maximized or minimized problem:
# 1 is for minimized problem, -1 is for maximized problem.
sense = problem.objective.sense.value

# convert a constant part of the objective function into Hamiltonian.
offset = problem.objective.constant * sense

# convert linear parts of the objective function into Hamiltonian.
linear = np.zeros(num_vars)
for idx, coef in problem.objective.linear.to_dict().items():
weight = coef * sense / 2
linear[idx] -= weight
offset += weight

# convert quadratic parts of the objective function into Hamiltonian.
quad = np.zeros((num_vars, num_vars))
for (i, j), coef in problem.objective.quadratic.to_dict().items():
weight = coef * sense / 4
if i == j:
linear[i] -= 2 * weight
offset += 2 * weight
else:
quad[i, j] += weight
linear[i] -= weight
linear[j] -= weight
offset += weight
# Generate the decision variable terms in terms of Ising variables (+1 or -1)
offset, linear, quad = self._generate_ising_terms(problem)

# Find variable partition (a graph coloring is sufficient)
variable_partition = self._find_variable_partition(quad)
Expand Down
3 changes: 1 addition & 2 deletions qrao/magic_rounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,7 @@ def _sample_bases_weighted(self, q2vars, trace_values):
mpm_pmp = (1-x) * y * (1-z) + x * (1-y) * z
pmm_mpp = x * (1-y) * (1-z) + (1-x) * y * z
# fmt: on
probs = [ppp_mmm, ppm_mmp, mpm_pmp, pmm_mpp]
basis_probs.append(probs)
basis_probs.append([ppp_mmm, ppm_mmp, mpm_pmp, pmm_mpp])

bases = [
[self.rng.choice(4, size=1, p=probs)[0] for probs in basis_probs]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_magic_rounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def test_dv_counts(self):
]
self.assertTrue(np.all(np.array(ref) == np.array(solns)))

def test_sample_bases_weighted(self):
def test_sample_bases_weighted(self): # pylint: disable=too-many-locals
"""
There are a few settings of the trace values which
cause the magic basis sampling probabilities to be deterministic.
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ commands =
envdir = .tox/lint
commands =
black --check .
pylint -rn --py-version=3.6 --disable=fixme,invalid-name,missing-function-docstring,protected-access,no-member,pointless-statement,too-many-locals,useless-super-delegation,consider-using-f-string,too-many-branches,consider-using-enumerate,duplicate-code qrao tests
pylint -rn --py-version=3.6 --disable=fixme,invalid-name,missing-function-docstring,protected-access,no-member,pointless-statement,useless-super-delegation,consider-using-f-string,too-many-branches,consider-using-enumerate,duplicate-code qrao tests
nbqa pylint -rn --py-version=3.6 --disable=missing-module-docstring,wrong-import-order,wrong-import-position,line-too-long,invalid-name,missing-function-docstring,no-member,pointless-statement,consider-using-f-string,duplicate-code docs/ tests/
mypy qrao tests

Expand Down

0 comments on commit eb9c418

Please sign in to comment.