ts-quantum
Version:
TypeScript library for quantum mechanics calculations and utilities
159 lines • 5.26 kB
JavaScript
/**
* SpinNetworkBuilder provides a fluent API for constructing spin networks -
* quantum graphs with SU(2) spins on edges and intertwiner vertices.
*/
import { QuantumGraph } from '../../qgraph/QuantumGraph';
/**
* Builder class for creating spin network quantum graphs
*/
export class SpinNetworkBuilder {
options;
graph;
constructor(options = {}) {
this.options = {
defaultSpin: 0.5,
computeIntertwiners: true,
...options
};
this.graph = new QuantumGraph();
}
/**
* Create a new SpinNetworkBuilder instance
*/
static create(options = {}) {
return new SpinNetworkBuilder(options);
}
/**
* Add a vertex with an intertwiner space based on incident edge spins
*/
addVertex(id, position) {
this.graph.addNode({
id,
type: 'intertwiner',
properties: {
position,
// Will be computed when adding edges
intertwiners: [],
// Hilbert space dimension determined by edge spins
dimension: 0
}
});
return this;
}
/**
* Add a quantum edge with specified spin between vertices
*/
addEdge(sourceId, targetId, spin) {
const edgeSpin = spin ?? this.options.defaultSpin ?? 0.5;
if (!Number.isFinite(edgeSpin) || edgeSpin < 0) {
throw new Error('Spin value must be a non-negative number');
}
this.graph.addEdge({
id: `${sourceId}-${targetId}`,
sourceId,
targetId,
type: 'quantum',
directed: false,
properties: {
spin: edgeSpin,
// Hilbert space dimension is 2j + 1 for spin j
dimension: 2 * edgeSpin + 1
}
});
// If enabled, update intertwiner spaces for affected vertices
if (this.options.computeIntertwiners) {
this.updateIntertwiners(sourceId);
this.updateIntertwiners(targetId);
}
return this;
}
/**
* Update the intertwiner space for a vertex based on incident edge spins
*/
updateIntertwiners(nodeId) {
const node = this.graph.getNode(nodeId);
if (!node || node.type !== 'intertwiner') {
return;
}
// Get incident edges and their spins
const edges = this.graph.getConnectedEdges(nodeId);
const spins = edges.map((edge) => edge.properties.spin);
// Calculate allowed intertwiner values based on spins
// TODO: Implement full intertwiner space calculation using packages/quantum tensor and angular momentum
const intertwiners = this.calculateAllowedIntertwiners(spins);
this.graph.addNode({
...node,
properties: {
...node.properties,
intertwiners,
dimension: intertwiners.length
}
});
}
/**
* Calculate the allowed intertwiner values for a set of spins
*/
calculateAllowedIntertwiners(spins) {
// TODO: Replace this placeholder with proper calculation
// For now just return a basic set based on number of edges
return [0];
}
/**
* Build and return the completed spin network
*/
build() {
return this.graph;
}
// Static factory methods for common network types
/**
* Create a 2D lattice spin network
*/
static createLattice2D(options) {
const { rows = 2, cols = 2, periodic = false, defaultSpin = 0.5 } = options;
const builder = SpinNetworkBuilder.create({ defaultSpin });
// Create vertices
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const id = `${i}-${j}`;
builder.addVertex(id, { x: j, y: i });
// Add edges
if (j > 0) {
builder.addEdge(`${i}-${j - 1}`, id);
}
if (i > 0) {
builder.addEdge(`${i - 1}-${j}`, id);
}
// Add periodic boundary edges if enabled
if (periodic) {
if (j === cols - 1) {
builder.addEdge(id, `${i}-0`);
}
if (i === rows - 1) {
builder.addEdge(id, `0-${j}`);
}
}
}
}
return builder.build();
}
/**
* Create a chain of spins with nearest-neighbor coupling
*/
static createChain(n, options) {
const { periodic = false, defaultSpin = 0.5 } = options;
const builder = SpinNetworkBuilder.create({ defaultSpin });
// Create vertices
for (let i = 0; i < n; i++) {
builder.addVertex(`${i}`, { x: i, y: 0 });
if (i > 0) {
builder.addEdge(`${i - 1}`, `${i}`);
}
}
// Add periodic boundary if enabled
if (periodic && n > 1) {
builder.addEdge(`${n - 1}`, '0');
}
return builder.build();
}
}
//# sourceMappingURL=spinNetwork.js.map