A quantum computing implementation for solving Boolean Satisfiability (SAT) problems using Grover's algorithm and quantum counting techniques.
The Boolean Satisfiability Problem (SAT) is one of the most fundamental problems in computer science and the first problem proven to be NP-complete. This project implements a quantum approach to solving SAT problems, potentially offering quadratic speedup over classical algorithms for certain instances.
The SAT problem asks whether there exists an assignment of boolean values (true/false) to variables that makes a given boolean formula evaluate to true. For example, given the formula (x1 OR x2) AND (NOT x1 OR x3), we need to find values for x1, x2, and x3 that make the entire expression true.
Classical SAT solvers can take exponential time in the worst case. Quantum algorithms like Grover's search can provide a quadratic speedup, reducing the search complexity from O(2^n) to O(2^(n/2)) for n variables.
The parser reads SAT problems in the standard DIMACS CNF (Conjunctive Normal Form) format:
Understanding CNF Format:
- CNF means the formula is a conjunction (AND) of clauses, where each clause is a disjunction (OR) of literals
- Literals are either variables (positive) or their negations (negative)
- Example:
(x1 OR NOT x2) AND (x2 OR x3)is in CNF form
DIMACS Format Structure:
c This is a comment line
p cnf 3 2 # Problem line: 3 variables, 2 clauses
1 -2 0 # Clause 1: (x1 OR NOT x2)
2 3 0 # Clause 2: (x2 OR x3)
The parser extracts:
- Number of variables and clauses
- List of clauses as integer arrays
- Generates human-readable boolean expressions
Before solving the SAT problem, we need to estimate how many solutions exist. This is crucial for optimizing Grover's algorithm.
How Quantum Counting Works:
Think of quantum counting like estimating the number of marked items in a database without examining each item individually. The algorithm uses:
- Quantum Phase Estimation: Creates superposition states and applies controlled operations
- Grover Operator: Amplifies the amplitude of satisfying assignments
- Inverse Quantum Fourier Transform: Extracts phase information to estimate solution count
Mathematical Foundation:
- If M solutions exist out of N total possibilities, Grover's operator rotates the state vector by angle θ where sin²(θ) = M/N
- By measuring the phase, we can estimate θ and hence M
The main solver uses a modified Grover's algorithm with partial diffusion operator.
Algorithm Steps:
- Initialization: Create uniform superposition of all possible variable assignments
- Oracle Construction: Build quantum circuit that marks satisfying assignments
- Amplitude Amplification: Use partial diffusion to amplify probability of correct solutions
- Measurement: Extract the most probable solutions
Understanding the Oracle:
The oracle is the heart of the quantum SAT solver. It works by:
- Using ancillary qubits to evaluate each clause
- Applying De Morgan's laws to convert OR operations to AND operations (easier to implement the clause by mcx gate)
- Marking states where all clauses are satisfied
Why Partial Diffusion?
Traditional Grover's algorithm assumes we know exactly how many solutions exist. Partial diffusion is more robust when our estimate might be imperfect, preventing the algorithm from "overshooting" and reducing success probability.
- Literal Register: Stores the boolean values of variables (x1, x2, ..., xn)
- Clause Register: Auxiliary qubits for evaluating individual clauses
- Ancillary Register: Workspace for final SAT evaluation
- Phase Register: Used in quantum counting for phase estimation
- Classical Register: Stores measurement results
- Multi-Controlled X Gates: Implement logical AND operations
- Hadamard Gates: Create and manipulate superposition states
- Controlled Unitary Operations: Apply Grover operators conditionally
- Quantum Fourier Transform: Extract phase information in counting
# 1. Parse DIMACS CNF file
variables_number, clauses_number, cnf_clauses_list = dimacs_cnf_files_parser()
# 2. Estimate number of solutions using quantum counting
estimated_solutions_number = quantum_counting(variables_number, clauses_number, cnf_clauses_list)
# 3. Solve SAT problem using partial diffusion
pd_sat_solver(variables_number, clauses_number, cnf_clauses_list, estimated_solutions_number)Converting CNF clauses to quantum circuits requires careful application of De Morgan's laws:
(A OR B) = NOT(NOT A AND NOT B)- This allows us to use multi-controlled X gates (which implement AND) to evaluate OR clauses
Quantum circuits must be reversible, so intermediate computations need to be "uncomputed" to avoid entanglement with ancillary qubits. The solver carefully reverses operations to clean up workspace qubits.
The number of Grover iterations is calculated as: π/4 × √(N/M)
where N is the total search space size and M is the estimated number of solutions.
The project includes various DIMACS CNF test files:
2vsat.cnf,3vsat.cnf: Simple satisfiable instancesu3v6c.cnf,u4v8c.cnf: Unsatisfiable instances for testingmax.cnf,max4.cnf: Maximum satisfiability problems
import os, sys, math, itertools
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, transpile
from qiskit.circuit.library import QFTGate, UnitaryGate
from qiskit_aer import AerSimulator
from IPython.display import display
from qiskit.visualization import plot_histogramThe solver provides:
- Circuit Visualization: Shows the quantum circuit structure
- Histogram Plots: Display measurement probability distributions
- Solution Assignments: Boolean variable assignments that satisfy the formula
- Confidence Percentages: How often each solution appeared in measurements
This implementation demonstrates several important quantum computing concepts:
Quantum Parallelism: How superposition allows simultaneous evaluation of all possible variable assignments
Amplitude Amplification: The principle behind Grover's algorithm and how it provides quadratic speedup
Quantum Phase Estimation: A fundamental subroutine used in many quantum algorithms
Reversible Computing: How quantum circuits maintain unitarity through careful uncomputation
The quantum SAT solver builds upon several key theoretical results:
Grover's Search Algorithm: Provides quadratic speedup for unstructured search problems
Quantum Counting: Allows estimation of solution space size without exhaustive search
Amplitude Amplification: Generalizes Grover's algorithm to arbitrary initial states and success probabilities
This implementation serves as both a practical tool for small SAT instances and an educational demonstration of how quantum algorithms can be applied to classical computational problems.
This project was developed for QCIC 766 - Research Project course, Professional Master in Quantum Computing and Quantum Information, demonstrating the application of quantum algorithms to NP-complete problems.
