Skip to main contentIBM Quantum Documentation
This page is from an old version of Qiskit SDK. Go to the latest version.

LogicalExpressionOracle

class LogicalExpressionOracle(expression, optimization=False, mct_mode='basic')

GitHub

The Logical Expression Quantum Oracle.

The Logical Expression Oracle, as its name suggests, constructs circuits for any arbitrary input logical expressions. A logical expression is composed of logical operators & (AND), | (OR), ~ (NOT), and ^ (XOR), as well as symbols for literals (variables). For example, ‘a & b’, and (v0 | ~v1) ^ (~v2 & v3) are both valid string representation of boolean logical expressions.

For convenience, this oracle, in addition to parsing arbitrary logical expressions, also supports input strings in the DIMACS CNF format, which is the standard format for specifying SATisfiability (SAT) problem instances in Conjunctive Normal Form (CNF), which is a conjunction of one or more clauses, where a clause is a disjunction of one or more literals.

The following is an example of a CNF expressed in DIMACS format:

c This is an example DIMACS CNF file with 3 satisfying assignments: 1 -2 3, -1 -2 -3, 1 2 -3.
p cnf 3 5
-1 -2 -3 0
1 -2 3 0
1 2 -3 0
1 -2 -3 0
-1 2 3 0

The first line, following the c character, is a comment. The second line specifies that the CNF is over three boolean variables — let us call them x1,x2,x3x_1, x_2, x_3, and contains five clauses. The five clauses, listed afterwards, are implicitly joined by the logical AND operator, \land, while the variables in each clause, represented by their indices, are implicitly disjoined by the logical OR operator, lorlor. The - symbol preceding a boolean variable index corresponds to the logical NOT operator, lnotlnot. Character 0 (zero) marks the end of each clause. Essentially, the code above corresponds to the following CNF:

(¬x1¬x2¬x3)(x1¬x2x3)(x1x2¬x3)(x1¬x2¬x3)(¬x1x2x3)(\lnot x_1 \lor \lnot x_2 \lor \lnot x_3) \land (x_1 \lor \lnot x_2 \lor x_3) \land (x_1 \lor x_2 \lor \lnot x_3) \land (x_1 \lor \lnot x_2 \lor \lnot x_3) \land (\lnot x_1 \lor x_2 \lor x_3).

This is an example showing how to search for a satisfying assignment to an SAT problem encoded in DIMACS using the Logical Expression oracle with the Grover algorithm.

Logic expressions, regardless of the input formats, are parsed and stored as Abstract Syntax Tree (AST) tuples, from which the corresponding circuits are constructed. The oracle circuits can then be used with any oracle-oriented algorithms when appropriate. For example, an oracle built from a DIMACS input can be used with the Grover’s algorithm to search for a satisfying assignment to the encoded SAT instance.

By default, the Logical Expression oracle will not try to apply any optimization when building the circuits. For any DIMACS input, the constructed circuit truthfully recreates each inner disjunctive clauses as well as the outermost conjunction; For other arbitrary input expression, It only tries to convert it to a CNF or DNF (Disjunctive Normal Form, similar to CNF, but with inner conjunctions and a outer disjunction) before constructing its circuit. This, for example, could be good for educational purposes, where a user would like to compare a built circuit against their input expression to examine and analyze details. However, this often leads to relatively deep circuits that possibly also involve many ancillary qubits. The oracle therefore, provides the option to try to optimize the input logical expression before building its circuit.

Parameters

  • expression (str) – The string of the desired logical expression. It could be either in the DIMACS CNF format, or a general boolean logical expression, such as ‘a ^ b’ and ‘v[0] & (~v[1] | v[2])’
  • optimization (bool) – Boolean flag for attempting logical expression optimization
  • mct_mode (str) – The mode to use for building Multiple-Control Toffoli.

Raises

AquaError – Invalid input


Attributes

ancillary_register

returns ancillary register

circuit

output_register

returns output register

variable_register

returns variable register


Methods

construct_circuit

LogicalExpressionOracle.construct_circuit()

construct circuit

evaluate_classically

LogicalExpressionOracle.evaluate_classically(measurement)

evaluate classically

Was this page helpful?
Report a bug or request content on GitHub.