qiskit-documentation/docs/api/qiskit/0.26/qiskit.aqua.components.orac...

116 lines
8.6 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: LogicalExpressionOracle
description: API reference for qiskit.aqua.components.oracles.LogicalExpressionOracle
in_page_toc_min_heading_level: 1
python_api_type: class
python_api_name: qiskit.aqua.components.oracles.LogicalExpressionOracle
---
# qiskit.aqua.components.oracles.LogicalExpressionOracle
<Class id="qiskit.aqua.components.oracles.LogicalExpressionOracle" isDedicatedPage={true} github="https://github.com/qiskit-community/qiskit-aqua/tree/stable/0.9/qiskit/aqua/components/oracles/logical_expression_oracle.py" signature="LogicalExpressionOracle(expression, optimization=False, mct_mode='basic')" modifiers="class">
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](http://www.satcompetition.org/2009/format-benchmarks2009.html), which is the standard format for specifying SATisfiability (SAT) problem instances in [Conjunctive Normal Form (CNF)](https://en.wikipedia.org/wiki/Conjunctive_normal_form), 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:
```python
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 $x_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, $lor$. The $-$ symbol preceding a boolean variable index corresponds to the logical NOT operator, $lnot$. Character 0 (zero) marks the end of each clause. Essentially, the code above corresponds to the following CNF:
$(\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.](https://github.com/Qiskit/qiskit-tutorials-community/blob/master/optimization/grover.ipynb)
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 Grovers 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**](qiskit.aqua.AquaError "qiskit.aqua.AquaError") Invalid input
### \_\_init\_\_
<Function id="qiskit.aqua.components.oracles.LogicalExpressionOracle.__init__" signature="__init__(expression, optimization=False, mct_mode='basic')">
**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**](qiskit.aqua.AquaError "qiskit.aqua.AquaError") Invalid input
</Function>
## Methods
| | |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
| [`__init__`](#qiskit.aqua.components.oracles.LogicalExpressionOracle.__init__ "qiskit.aqua.components.oracles.LogicalExpressionOracle.__init__")(expression\[, optimization, mct\_mode]) | **type expression**`str` |
| [`construct_circuit`](#qiskit.aqua.components.oracles.LogicalExpressionOracle.construct_circuit "qiskit.aqua.components.oracles.LogicalExpressionOracle.construct_circuit")() | construct circuit |
| [`evaluate_classically`](#qiskit.aqua.components.oracles.LogicalExpressionOracle.evaluate_classically "qiskit.aqua.components.oracles.LogicalExpressionOracle.evaluate_classically")(measurement) | evaluate classically |
## Attributes
| | |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------- |
| [`ancillary_register`](#qiskit.aqua.components.oracles.LogicalExpressionOracle.ancillary_register "qiskit.aqua.components.oracles.LogicalExpressionOracle.ancillary_register") | returns ancillary register |
| [`circuit`](#qiskit.aqua.components.oracles.LogicalExpressionOracle.circuit "qiskit.aqua.components.oracles.LogicalExpressionOracle.circuit") | |
| [`output_register`](#qiskit.aqua.components.oracles.LogicalExpressionOracle.output_register "qiskit.aqua.components.oracles.LogicalExpressionOracle.output_register") | returns output register |
| [`variable_register`](#qiskit.aqua.components.oracles.LogicalExpressionOracle.variable_register "qiskit.aqua.components.oracles.LogicalExpressionOracle.variable_register") | returns variable register |
### ancillary\_register
<Attribute id="qiskit.aqua.components.oracles.LogicalExpressionOracle.ancillary_register">
returns ancillary register
</Attribute>
### circuit
<Attribute id="qiskit.aqua.components.oracles.LogicalExpressionOracle.circuit" />
### construct\_circuit
<Function id="qiskit.aqua.components.oracles.LogicalExpressionOracle.construct_circuit" signature="construct_circuit()">
construct circuit
</Function>
### evaluate\_classically
<Function id="qiskit.aqua.components.oracles.LogicalExpressionOracle.evaluate_classically" signature="evaluate_classically(measurement)">
evaluate classically
</Function>
### output\_register
<Attribute id="qiskit.aqua.components.oracles.LogicalExpressionOracle.output_register">
returns output register
</Attribute>
### variable\_register
<Attribute id="qiskit.aqua.components.oracles.LogicalExpressionOracle.variable_register">
returns variable register
</Attribute>
</Class>