Summary
This repository contains helper functions for: building/decoding signed-binary encodings of lattice vectors, running simple Gauss-sieve routines, converting lattice vectors to Ising/QUBO representations, running a Coherent Ising Machine (CIM) pipeline, and several utilities (LLL wrap, plotting, brute-force Ising). The code is intended to support experimentation on lattice problems (e.g. LWE/primal lattices) using Ising solvers and CIM-style heuristics.
- Quick overview
- Requirements & installation
- Important conventions (rows vs columns, encoding)
- Key functions (API reference + examples)
- Typical workflows & examples
- Performance & safety notes
- License & author
- The main file is IsingLWE.py
build_meta_with_fixed(B, bits, fixed_idx, fixed_val, convention)— build ametadescriptor for signed-binary encoding of integer coefficient vectorsu(with the option to fix coordinate(s)).decode_bits_to_u_with_fixed(x_bits, meta)— decode an m-bit vector into full integeruincluding fixed entries.build_all_coord_lookups(meta)/_build_value_to_bits_lookup_for_coord— internal helpers to map integeru[i]values to bit patterns for each free coordinate.u_to_bits_vector/v_to_u_from_B— convert betweenu, bitsx, and lattice vectorv(via basisB).gauss_sieve/gauss_sieve2— two versions of a basic Gauss-sieve implementation returning a list of short lattice vectors and ahistorydict for plotting.plot_sieving_progress— visualize min/avg/max squared norms during the sieve.lll_reduce_basis— wrapper aroundfpylll.LLLto return an LLL-reduced integer matrix.gauss_vs_to_ising_states— high level: converts Gauss-sieve output vectorsVinto bit vectors, spins and Ising dicts, along withuand norms.IsingLWE— pipeline: build QUBO frommeta, convert to Ising, run CIM (viaIsingMachine.CFC) and refine withSteepestDescentSamplerfrom dwave-package. Returns candidate lattice vectors and best norm.simulate_snn_cim— a simple ODE-based SNN-CIM Euler integrator useful for prototyping.brute_force_ising— exact minimizer of small Ising Hamiltonians (warning: exponential in N).enumerate_all_with_fixed— exact enumeration of all2^mbit patterns with fixed coordinates; useful for small instances and debug.- Utilities for converting between dict-samples and arrays:
dicts_to_spin_list,bits_to_spin_dicts,spins_array_to_u_v,s0_to_initial_states,infer_order_from_keys.
Minimum required libraries (approx):
- Python 3.8+
- numpy
- matplotlib
- fpylll
- dimod
- dwave-ocean-sdk (for
dwave.samplers.SteepestDescentSampler) — optional if you skip the refinement step singlequboandIsingMachine— local modules used for QUBO assembly and CIM solver (CFC).- PyTorch (used indirectly by the CFC solver in
run_cim) if the solver implementation uses it.
Install with pip (example):
pip install numpy matplotlib fpylll dimod dwave-ocean-sdk
# plus local requirements for IsingMachine and singlequboNote:
fpylllmay require system-level dependencies. Install via conda if preferred:conda install -c conda-forge fpylll.
B(basis): ifconvention == 'rows', code assumes row-basis where lattice vectorv = u @ Band GramG = B @ B.T. Ifconvention == 'columns',v = B @ uandG = B.T @ B.metahas keys:n(dimension),bits(bits per free coord),var_index_mapmapping(i,k)to bit-index,bit_weightsgiving signed weights per bit,m(total free bits),G,convention,fixed_indices,fixed_vals.x_bitsis an array of lengthmwith values in{0,1}. Conversion to spins:s = 1 - 2*x_bits(sox=0 -> s=+1,x=1 -> s=-1).Uis integer vector lengthn.Vis lattice vector lengthn.
Returns: meta dict.
Example:
meta = build_meta_with_fixed(B, bits=4, fixed_idx=B.shape[0]-1, fixed_val=1, convention='rows')This fixes the last coordinate to 1 and encodes the others using bits signed-binary (MSB negative) per coordinate.
Input: x_bits length m -> returns full integer u length n.
Example:
u = decode_bits_to_u_with_fixed(x_bits, meta)
v = (u @ B) # if convention rowsRecover integer u from v and B. Returns (u_rounded, ok_flag).
Example:
u, ok = v_to_u_from_B(v, B, 'rows')Convert full u to x_bits using precomputed coordinate lookups (from build_all_coord_lookups). Returns (x_bits, ok) where ok==False if some coordinate value is not representable by available bits.
Example:
lookups = build_all_coord_lookups(meta)
x_bits, ok = u_to_bits_vector(u, meta, lookups)A robust version that accepts ndarray or list and returns (processed_list, history) where processed_list is list of short vectors and history contains min_norm, max_norm, avg_norm per step.
Example:
processed, history = gauss_sieve2(B, initial_vectors)
plot_sieving_progress(history)High-level conversion used after running a Gauss sieve. Accepts V shape (num, n) or list. Returns dictionary with X_bits, S_spins, ising_dicts, U, V, norms_sq, valid_mask, lookups.
Example:
out = gauss_vs_to_ising_states(processed, B, meta)
print(out['X_bits'].shape, out['ising_dicts'][0])Builds QUBO from meta and runs the CIM solver pipeline (IsingMachine.CFC) and a steepest-descent refinement. Returns (V_candidates, best_norm).
Minimal usage:
V, best_norm = run_cim(B, meta, NUM_VECTORS=500, iters=500)Important:
run_cimdepends onsinglequbo.build_qubo_full,IsingMachine.CFCand a working GPU setup if the solver uses GPU. If you don't have these, either stub them or replace the solver call with another sampler.
Exactly finds ground states for small N. Do not use for N > ~24 unless you have lots of memory/cpu.
Example:
res = brute_force_ising(J, h)
print(res['ground_energy'], res['ground_states'].shape)Euler integration of a coupled ODE system modeling a SNN-CIM. Returns (x_hist, k_hist, energy_hist) useful for diagnostics/plots.
Example:
x_hist, k_hist, E = simulate_snn_cim(J, n_iter=200, dt=0.01)
plt.plot(E)A) Small exact enumeration (debug)
- Build
metawithbitssmall (e.g. 3). 2. Useenumerate_all_with_fixed(B, meta)to list all (2^m) configurations and energies.
B) Gauss-sieve → convert → Ising → CIM
- Prepare
initial_vectorsfor the lattice (random or structured). 2.processed, history = gauss_sieve2(B, initial_vectors). 3.meta = build_meta_with_fixed(B, bits=4, fixed_idx=..., fixed_val=...). 4.out = gauss_vs_to_ising_states(processed, B, meta). 5. Optionally runrun_cim(B, meta)to get improved candidates.
C) Direct CIM search on QUBO encoding
- From
meta, callbuild_qubo_full(meta)(provided in localsinglequbomodule). 2. Userun_cimwhich wraps the whole pipeline.
brute_force_isingscales as2^Nmemory/time — usemax_enumeration_bitsto avoid accidental explosions.- The Gauss-sieve implementations are educational/small-scale: for production-level large sieving use optimized libraries.
v_to_u_from_Busesnp.linalg.solveand floating rounding — ensureBis well-conditioned or use exact/integer linear algebra for very large integers.
MIT license. Author: Mahmood Hasani