qlasskit

Qlasskit

  • Quickstart
  • How it works
  • Supported python subset
  • Parameters
  • Algorithms
  • Exporting to other frameworks
  • Binary Quadratic Model, Qubo & Ising
  • Decompiler and Circuit Optimizer
  • API
  • CLI Tools

Examples

  • Grover search
  • Grover search: SAT problem
  • Grover search: subset problem
  • Grover search: hash function preimage attack
  • Grover search: sudoku solver
  • Grover: factorize number
  • Simon function periodicity
  • Deutsch Jozsa algorithm
  • Bernstein Vazirani algorithm
  • Unitary of qlasskit function
  • Working with big circuits
  • Solving TSP as Binary Quadratic Model
qlasskit
  • Grover search
  • View page source

Grover search

Qlasskit offer a class to easily perform a Grover search over a qlasskit oracle. First, we define a function named and_all that returns True iff all the element of an input list a_list are True. We want to use a Grover search to find the input value that led to a True result of the function.

from qlasskit import qlassf, Qlist


@qlassf
def and_all(a_list: Qlist[bool, 4]) -> bool:
    r = True
    for i in a_list:
        r = r and i
    return r

The qlasskit compiler will produce an optimized quantum circuit performing the given function.

and_all.export("qiskit").draw("mpl")
_images/749354d07e433a775caa1c63bb8e1109fadae20a78d6a16014f0b4d787b5252f.png

We now can use our quantum function as an oracle for a Grover search. For instance, we want to find the input value that yeld to a True value of the function:

from qlasskit.algorithms import Grover

q_algo = Grover(and_all, True)

Qlasskit prepares the quantum circuit for the Grover search:

qc = q_algo.export("qiskit")
qc.draw("mpl")
_images/d054090ac2c1c024f71cdfb0da510a863ac2b589869582cdb089021cce30d73c.png

Then we use our prefered framework and simulator for sampling the result; this is an example using qiskit with aer_simulator.

The Grover class, along with all circuit wrappers in qlasskit, provides utilities to encode inputs and decode outputs from a quantum circuit using the high level type definitions. In the output histogram, it’s now evident that the input leading to a True result in the and_all function is a list where all elements are set to True, aligning with our expectations.

from qiskit import QuantumCircuit, transpile
from qiskit.visualization import plot_histogram
from qiskit_aer import AerSimulator

qc.measure_all()
simulator = AerSimulator()
circ = transpile(qc, simulator)
result = simulator.run(circ).result()
counts = result.get_counts(circ)

counts_readable = q_algo.decode_counts(counts)
plot_histogram(counts_readable)
_images/f599b5c8bb2c975eb5a6406b886660132cfda128045bfef8507229aa3f923903.png
Previous Next

© Copyright 2023-2024, Davide Gessa (dakk).

Built with Sphinx using a theme provided by Read the Docs.