PhaseOracle
class qiskit.circuit.library.PhaseOracle(expression, var_order=None)
Bases: QuantumCircuit
Phase Oracle.
The Phase Oracle object constructs circuits for any arbitrary input logical expressions. A logical expression is composed of logical operators & (logical AND), | (logical OR), ~ (logical NOT), and ^ (logical 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.
A phase oracle for a boolean function f(x) performs the following quantum operation:
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. See qiskit.circuit.library.phase_oracle.PhaseOracle.from_dimacs_file()
.
From 16 variables on, possible performance issues should be expected when using the default synthesizer.
Parameters
- expression (str) – A Python-like boolean expression.
- var_order (list[str] | None) – A list with the order in which variables will be created. (default: by appearance)
Attributes
name
Type: str
A human-readable name for the circuit.
Example
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2, name="my_circuit")
print(qc.name)
my_circuit
Methods
evaluate_bitstring
evaluate_bitstring(bitstring)
Evaluate the oracle on a bitstring. This evaluation is done classically without any quantum circuit.
Parameters
bitstring (str) – The bitstring for which to evaluate. The input bitstring is expected to be in little-endian order.
Returns
True if the bitstring is a good state, False otherwise.
Return type
from_dimacs_file
classmethod from_dimacs_file(filename)
Create a PhaseOracle from the string in the DIMACS format.
It is possible to build a PhaseOracle from a file in 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 the DIMACS format:
c 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 , and contains five clauses. The five clauses, listed afterwards, are implicitly joined by the logical AND operator, , while the variables in each clause, represented by their indices, are implicitly disjoined by the logical OR operator, . The symbol preceding a boolean variable index corresponds to the logical NOT operator, . Character 0 (zero) marks the end of each clause. Essentially, the code above corresponds to the following CNF:
.
Parameters
filename (str) – A file in DIMACS format.
Returns
A quantum circuit with a phase oracle.
Return type