Swift 量子电路模拟器
到目前为止,代码主要基于以下内容: 《计算机科学家量子计算》,并参考了来自 《自动量子计算机编程:遗传编程方法》 的几个技巧。它还受到了 IBM Qiskit 的启发。
除了模拟器,还有一个遗传算法可以自动生成电路以及量子计算的其他一些有用算法。
使用方法
构建并使用量子电路
import SwiftQuantumComputing // for macOS
//: 1. Compose a list of quantum gates. Insert them in the same order
//: you want them to appear in the quantum circuit
let matrix = Matrix([[Complex.one, Complex.zero, Complex.zero, Complex.zero],
[Complex.zero, Complex.one, Complex.zero, Complex.zero],
[Complex.zero, Complex.zero, Complex.zero, Complex.one],
[Complex.zero, Complex.zero, Complex.one, Complex.zero]])
let gates = [
Gate.hadamard(target: 3),
Gate.controlledMatrix(matrix: matrix, inputs: [3, 4], control: 1),
Gate.controlledNot(target: 0, control: 3),
Gate.matrix(matrix: matrix, inputs: [3, 2]),
Gate.not(target: 1),
Gate.oracle(truthTable: ["01", "10"], target: 3, controls: [0, 1]),
Gate.phaseShift(radians: 0.25, target: 0)
]
//: 2. (Optional) Draw the quantum circuit to see how it looks
let drawer = MainDrawerFactory().makeDrawer()
drawer.drawCircuit(gates).get()
//: 3. Build the quantum circuit with the list of gates
let circuit = MainCircuitFactory().makeCircuit(gates: gates)
//: 4. Use the quantum circuit
let statevector = circuit.statevector().get()
print("Statevector: \(statevector)\n")
print("Probabilities: \(statevector.probabilities())\n")
print("Summarized probabilities: \(statevector.summarizedProbabilities())\n")
let groupedProbs = statevector.groupedProbabilities(byQubits: [1, 0],
summarizedByQubits: [4, 3, 2]).get()
print("Grouped probabilities: \(groupedProbs)")
print("Unitary: \(circuit.unitary().get())\n")
查看完整代码 Circuit.playground.
绘制量子电路
查看完整代码 Drawer.playground.
使用遗传算法自动生成量子电路
import SwiftQuantumComputing // for macOS
//: 1. Define a configuration for the genetic algorithm
let config = GeneticConfiguration(depth: (1..<50),
generationCount: 2000,
populationSize: (2500..<6500),
tournamentSize: 7,
mutationProbability: 0.2,
threshold: 0.48,
errorProbability: 0.000000000000001)
//: 2. Also the uses cases, i.e. the circuit outputs you want to get
//: when the oracle is configured with the different truth tables
let cases = [
GeneticUseCase(emptyTruthTableQubitCount: 1, circuitOutput: "00"),
GeneticUseCase(truthTable: ["0", "1"], circuitOutput: "00"),
GeneticUseCase(truthTable: ["0"], circuitOutput: "10"),
GeneticUseCase(truthTable: ["1"], circuitOutput: "10")
]
//: 3. And which gates can be used to find a solution
let gates: [ConfigurableGate] = [HadamardGate(), NotGate()]
//: 4. Now, run the genetic algorithm to find/evolve a circuit that solves
//: the problem modeled with the use cases
let evolvedCircuit = MainGeneticFactory().evolveCircuit(configuration: config,
useCases: cases,
gates: gates).get()
print("Solution found. Fitness score: \(evolvedCircuit.eval)")
for useCase in cases {
//: 5. (Optional) Draw the solution (check `Sources` folder in Playground for the source code)
let evolvedGates = configureEvolvedGates(in: evolvedCircuit, with: useCase)
drawCircuit(with: evolvedGates, useCase: useCase)
//: 6. (Optional) Check how well the solution found meets each use case
//: (check `Sources` folder in Playground for the source code)
let probs = probabilities(in: evolvedGates, useCase: useCase)
print(String(format: "Use case: [%@]. Input: %@ -> Output: %@. Probability: %.2f %%",
useCase.truthTable.truth.joined(separator: ", "),
useCase.circuit.input,
useCase.circuit.output,
(probs[useCase.circuit.output] ?? 0.0) * 100))
}
在 Genetic.playground 中查看完整代码。
其他例子
查看以下游乐场以获取更多示例
- BernsteinVaziraniAlgorithm.playground - Bernstein–Vazirani 算法。
- DeutschAlgorithm.playground - Deutsch 算法。
- DeutschJozsaAlgorithm.playground - Deutsch-Jozsa 算法。
- GroverAlgorithm.playground - Grover's 算法。
- ShorAlgorithm.playground - Shor 算法。
- SimonPeriodicityAlgorithm.playground - Simon 的周期性算法。
其他算法
欧几里得算法:求两个整数的最大公约数
import SwiftQuantumComputing // for macOS
//: 1. Define two integers
let a = 252
let b = 105
//: 2. Use Euclidean solver to find greatest common divisor of these integers
let gcd = EuclideanSolver.findGreatestCommonDivisor(a, b)
print("Greatest common divisor of \(a) & \(b): \(gcd)")
在 EuclideanAlgorithm.playground 中查看完整代码。
连分数:找到一个给定的有理数的近似值
import SwiftQuantumComputing // for macOS
//: 1. Define rational value to approximate
let value = Rational(numerator: 15, denominator: 11)
//: 2. And a limit or maximum difference between approximation and original value
let limit = Rational(numerator: 1, denominator: 33)
//: 3. Use Continued Fractions solver to find a solution
let approximation = ContinuedFractionsSolver.findApproximation(of: value,
differenceBelowOrEqual: limit).get()
print("Approximation for \(value) (limit: \(limit)): \(approximation)")
在 ContinuedFractions.playground 中查看完整代码。
高斯消元法:求解异或方程组的解
import SwiftQuantumComputing // for macOS
//: 1. Define system of XOR equations:
//: * `x6 ^ x4 ^ x2 ^ x1 = 0`
//: * ` x4 ^ x0 = 0`
//: * `x6 ^ x5 ^ x2 ^ x0 = 0`
//: * ` x4 ^ x3 ^ x1 ^ x0 = 0`
//: * ` x5 ^ x3 ^ x0 = 0`
//: * ` x4 ^ x3 ^ x1 = 0`
//: * ` x5 ^ x4 ^ x2 ^ x1 ^ x0 = 0`
let equations = [
"1010110",
"0010001",
"1100101",
"0011011",
"0101001",
"0011010",
"0110111"
]
//: 2. Build Gaussian elimination solver
let solver = MainXorGaussianEliminationSolverFactory().makeSolver()
//: 3. Use solver
print("Solutions: \(solver.findActivatedVariablesInEquations(equations))")
完整代码位于 XorGaussianElimination.playground 中。
文档
项目的文档可以在 这里 找到。
Linux
如果运行在 Linux 上,此包依赖于 BLAS,更确切地说,是 Ubuntu。
这个依赖关系在 Package.swift
中通过与 CBLAS-Linux 反映出来,后者又期望找到以下文件: /usr/include/x86_64-linux-gnu/cblas-netlib.h
。因此,在安装了 BLAS (如果还没有安装) 之后
sudo apt-get install libblas-dev
检查 cblas-netlib.h
是否在预期位置。