Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

improve handling of logical expr optimization/simplification #821

Merged
merged 2 commits into from
Feb 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions qiskit/aqua/components/oracles/logical_expression_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import re

from sympy.parsing.sympy_parser import parse_expr
from sympy.logic.boolalg import to_cnf, BooleanTrue, BooleanFalse
from sympy.logic import simplify_logic
from sympy.logic.boolalg import is_cnf, is_dnf, BooleanTrue, BooleanFalse
from qiskit import QuantumCircuit, QuantumRegister

from qiskit.aqua import AquaError
Expand Down Expand Up @@ -171,14 +172,18 @@ def _process_expr(self):
self._num_vars = len(self._expr.binary_symbols)
self._lit_to_var = [None] + sorted(self._expr.binary_symbols, key=str)
self._var_to_lit = dict(zip(self._lit_to_var[1:], range(1, self._num_vars + 1)))
cnf = to_cnf(self._expr, simplify=self._optimization)

if isinstance(cnf, BooleanTrue):
if self._optimization or (not is_cnf(self._expr) and not is_dnf(self._expr)):
expr = simplify_logic(self._expr)
else:
expr = self._expr

if isinstance(expr, BooleanTrue):
ast = 'const', 1
elif isinstance(cnf, BooleanFalse):
elif isinstance(expr, BooleanFalse):
ast = 'const', 0
else:
ast = get_ast(self._var_to_lit, cnf)
ast = get_ast(self._var_to_lit, expr)

if ast[0] == 'or':
self._nf = DNF(ast, num_vars=self._num_vars)
Expand Down