All examples
Quantum computing

Fit a quantum circuit parameter

Use an explicit classical parameter shift update around a small experimental CPU quantum circuit.

Experimental module
quantum_variational.aner
// Experimental hybrid optimization: minimize the sampled expectation of Z.
// A one-qubit teaching problem; no claim of quantum advantage or NN autograd.
import aner.quantum

fn energy(theta: Float64, backend: QuantumBackend, seed: Int64) {
    return quantum.circuit(qubits: 1).ry(0, theta)
        .run(backend: backend, shots: 8192, seed: seed).sampled_z(0)
}

let backend = quantum.cpu()
let half_pi = 1.5707963267948966
var theta = 0.4
print("Initial sampled energy")
print(energy(theta, backend, 100))
for step in Range(40) {
    // Parameter shift for this single Ry rotation and Pauli-Z observable.
    let plus = energy(theta + half_pi, backend, 2026 + 2 * step)
    let minus = energy(theta - half_pi, backend, 2027 + 2 * step)
    let gradient = (plus - minus) / 2.0
    theta -= 0.3 * gradient
}
print("Final angle in radians")
print(theta)
print("Final sampled energy; the ideal minimum is -1")
print(energy(theta, backend, 9000))

Make it your experiment.

With a compatible development runtime installed, save this program as examples/quantum_variational.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/quantum_variational.aner
aner run examples/quantum_variational.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Use the updated native Aner development runtime with the experimental aner.quantum CPU simulator. Earlier packaged executables may not recognize this module. The complete examples run locally with no external service, credential or quantum hardware. Reserved GPU, cluster and physical hardware descriptors report unavailable; requested execution fails explicitly without fallback.

Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner