Factorization using Qlasskit and DWave Quantum Annealer

In the last release of Qlasskit, I introduced a new feature able to export a qlassf function to a binary quadratic model (as bqm, qubo or ising model). This feature introduces qlasskit to the realm of quantum annealer like the ones manufactered by DWave; in this blog post, we’ll explore this new feature, using Qlasskit and the DWave quantum annealer for prime factorization.

Instead of employing the traditional Shor algorithm utilized in circuit-base quantum computers, we opt to frame our problem as a minimization problem and we exploit the adiabatic quantum computing for searching a solution.

We begin defining a Qlasskit function called test_factor_generic which takes the number num to be factorized along with its two factors a and b as inputs. It returns 0 if a multiplied by b equals num.

from qlasskit import qlassf, Qint4, Qint3, Parameter

def test_factor_generic(num: Parameter[Qint4], a: Qint3, b: Qint3) -> Qint4:
    return num - (a * b)

Next, we bind the num parameter to the number 15 and convert the Qlasskit function to a binary quadratic model (BQM) using the to_bqm function.

test_factor = test_factor_generic.bind(num=15)
bqm = test_factor.to_bqm()

This BQM represents the optimization problem of finding the factors of 15.

Running the sampler

With our problem encoded into a BQM, we’re now poised to execute it on a real quantum annealer. Here’s how:

from dwave.system import DWaveSampler, EmbeddingComposite
from qlasskit.bqm import decode_samples

sampler = EmbeddingComposite(DWaveSampler())
sampleset = sampler.sample(bqm, num_reads=10)
decoded_samples = decode_samples(test_factor, sampleset)
best_sample = min(decoded_samples, key=lambda x: x.energy)

This code snippet runs the BQM on a DWave quantum annealer and prints the best solution found, which, as expected, yields {'a': 3, 'b': 5}, since 5 times 3 equals 15.

If you don’t have access to a DWave account, you can still run smaller problems like this one using a simulated annealing sampler as follows:

import neal

sa = neal.SimulatedAnnealingSampler()
sampleset = sa.sample(bqm, num_reads=10)
decoded_samples = decode_samples(test_factor, sampleset)
best_sample = min(decoded_samples, key=lambda x: x.energy)


In this post, we’ve demonstrated how to leverage Qlasskit and the DWave quantum annealer to factorize a number. Although this example is relatively straightforward, it underscores the potential of the qlasskit library in describing optimization problems using high-level abstractions.

Useful Links:


The Time I Built a Probabilistic Computer

5 minute read

In early 2023, I embarked on a journey to explore the field of probabilistic computing. This endeavor culminated in the construction of a hardware prototype,...

Back to Top ↑


Expanding the Commodore 64 Quantum Emulator

1 minute read

In a recent article I wrote, “Quantum Computing on a Commodore 64 in 200 Lines of BASIC”, published both on Medium and Hackaday.com, shows a two-qubit quantu...

Back to Top ↑


OCaml and Quantum Computing

1 minute read

Qiskit is a python SDK developed by IBM and allows everyone to create quantum circuits, simulate them locally and also run the quantum circuit on a real quan...

Yallo, a new Tezos language

2 minute read

As someone noticed from the previous post, last weeks I started to write a new programming language for Tezos smart contracts. This project was initially int...

King of Tezos: a smart-ponzi on Tezos

2 minute read

While writing a new programming language, it is often useful to write some real use-cases to test the syntax, the language expressiveness and the code cleann...

Favorite dev quote

less than 1 minute read

Documentation is like sex: when it is good, it is very, very good; and when it is bad, it is better than nothing

New blog

less than 1 minute read

This is my new blog, based on jekyll. I’ll soon import old posts from my old blog.

Back to Top ↑


Contractvm: decentralized applications on Bitcoin

less than 1 minute read

Contractvm is a general-purpose decentralized framework based on blockchain. The framework allows to implement arbitrary decentralized applications in an eas...

Back to Top ↑


Dices provably fair - Nonce overflow vulnerability

less than 1 minute read

Most of bitcoin dice software use a system to prove the fair play of the server for each bet. Most of them implement this mechanism using two seed (server se...

Back to Top ↑


Apache2: redirect different domains to subfolder

less than 1 minute read

In the aim to merge two of my server on digitalocean, today I tried to write a mod_rewrite rule to redirect a secondary domain to a subfolder. After one hour...

Back to Top ↑


MineML: F# miner

less than 1 minute read

MineML is a multithread CPU based bitcoin miner written in F#. At the moment it’s a slow implementation, but the class structure offers the possibility to im...

Back to Top ↑