Skip to content

A console tool to build quantum circuits and find solutions to quantum algorithms

License

Notifications You must be signed in to change notification settings

atten/quantum-solver

Repository files navigation

quantum-solver

A console tool to build quantum circuits and find solutions to quantum algorithms.

Install

apt-get install git openjdk-jdk21-headless
git clone

Usage

git pull || git rebase
./gradlew run 2> quantum-solver.log

Examples

Calculate output

Given specified input state and gates, produces solution (output state).

import org.qsolver.math.calculate
import org.qsolver.utils.parseGates
import org.qsolver.utils.parseVector
import org.qsolver.utils.toPrettyString

fun main() {
    calculate(
        qubits = 2,
        input = parseVector("1 | 0 | 0 | 0"),
        gates = parseGates("H H | Z I")
    )
        .toPrettyString()
        .let { println(it) }
}

Output:

0.5 | 0.5 | -0.5 | -0.5

Brute force solution

Given specified number of qubits, available gates and expected state (output), finds quantum circuit to reach that state. The algorithm checks all available combinations until first solution found.

It may be extremely slow when number of qubits or gates starts to grow (3 qubits circuit may take hours to brute force all combinations, 4 qubits - several days, 5 qubits - years). To estimate number of combinations and ETA, use startBenchmark = true.

Brute force algorithm uses CPU threads to parallel computations. All gate combinations are evenly distributed between threads. Exact number of threads can be specified with cores argument (default = number of logical CPU cores).

import org.qsolver.gates.Gates
import org.qsolver.solver.BruteForceSolver
import org.qsolver.utils.parseVector

fun main() {
    BruteForceSolver(
        qubits = 2,
        availableGates = setOf(
            Gates.I,
            Gates.H,
            Gates.X,
            Gates.Z,
            Gates.S,
            Gates.K,
        ),
        input = parseVector("1 | 0 | 0 | 0"),
        initialState = listOf(
            Gates.H, Gates.H,
            Gates.K, Gates.H,
            null, null,
            null, null,
            null, null,
            null, null,
            null, null,
        ),
        output = parseVector("0.35 | 0.35i | 0.5 | 0.71i"),
    ).solve(startBenchmark = true)
}

Output:

Start performance benchmark...
Current CPU performance: 1404K iterations/sec...
Mutations limit: 60466176, ETA: 2026-02-15T09:01:29.531367007Z (PT43S)
Progress (thread5): 0.11431184270690443%
Progress (thread10): 0.22862368541380887%
Progress (thread15): 0.3429355281207133%
Progress (thread1): 0.5520838625548273%
Progress (thread6): 0.6663957052617318%
...
Progress (thread3): 86.33169062981591%
Progress (thread3): 88.97779809988315%
Progress (thread3): 91.62390556995038%
Solution: Solution(gates=HH | KH | HK | KX | XS | XK | II, proximityRate=0.0, output=0.35 | 0.35i | 0.5 | 0.71i)
Spent: 26s

Gradient descent solution

Given specified number of qubits, available gates and expected state (output), finds quantum circuit to reach that state. The algorithm checks available combinations by steps until first solution found. Each step includes reduced number of combinations (specified by rowsPerIteration param) and produces top 10 outputs sorted by proximity to expected solution. Maximum number of steps is specified by rowsLimit param (default = no limit).

import org.qsolver.gates.Gates
import org.qsolver.solver.GradientDescendSolver
import org.qsolver.utils.parseVector

fun main() {
    GradientDescendSolver(
        qubits = 2,
        availableGates = setOf(
            Gates.I,
            Gates.H,
            Gates.X,
            Gates.Z,
            Gates.S,
            Gates.K,
        ),
        input = parseVector("1 | 0 | 0 | 0"),
        output = parseVector("0.35 | 0.35i | 0.5 | 0.71i"),
        cores = 8,
    ).solve()
}

Output:

Full solution: HH | HK | IS | IS | KH | XS | ZK
Total spent: 0s

References

Quantum Odyssey: https://store.steampowered.com/app/2802710/Quantum_Odyssey/

Matrix multiply: https://ru.onlinemschool.com/math/assistance/matrix/multiply/

About

A console tool to build quantum circuits and find solutions to quantum algorithms

Topics

Resources

License

Stars

Watchers

Forks

Languages