ts-quantum
Version:
TypeScript library for quantum mechanics calculations and utilities
119 lines • 4.47 kB
JavaScript
;
/**
* Intertwiner Tensor Representation
*
* This module provides tensor representations of intertwiner basis states,
* integrating with the StateVector framework for efficient sparse storage.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createIntertwinerTensor = exports.basisToTensor = void 0;
const stateVector_1 = require("../states/stateVector");
const basis_1 = require("./basis");
const math = __importStar(require("mathjs"));
/**
* Convert basis state to sparse tensor representation
*
* @param basis Intertwiner basis state to convert
* @returns Tensor representation with StateVector storage
*/
function basisToTensor(basis) {
const stateVector = basis.stateVector;
const dimensions = extractDimensions(stateVector);
validateTensorDimensions(dimensions);
// Create sparse representation (already handled by StateVector)
const sparseTensor = createSparseTensorRepresentation(stateVector);
return {
dimensions,
stateVector: sparseTensor,
basisState: basis
};
}
exports.basisToTensor = basisToTensor;
/**
* Create intertwiner tensor from edge spins and intermediate J
*
* @param edgeSpins Array of edge angular momentum values
* @param intermediateJ Intermediate coupling value
* @returns Tensor representation or null if invalid coupling
* @throws Error if not 4-valent node
*/
function createIntertwinerTensor(edgeSpins, intermediateJ) {
if (edgeSpins.length !== 4) {
throw new Error('Tensor creation currently supports only 4-valent nodes');
}
const [j1, j2, j3, j4] = edgeSpins;
const basisState = (0, basis_1.constructBasisVector)(j1, j2, j3, j4, intermediateJ);
if (!basisState) {
return null;
}
return basisToTensor(basisState);
}
exports.createIntertwinerTensor = createIntertwinerTensor;
// ==================== Helper Functions ====================
/**
* Validate tensor dimensions
*/
function validateTensorDimensions(dimensions) {
if (!Array.isArray(dimensions) || dimensions.length === 0) {
throw new Error('Dimensions must be non-empty array');
}
for (const [i, dim] of dimensions.entries()) {
if (!Number.isInteger(dim) || dim < 1) {
throw new Error(`Invalid dimension at index ${i}: ${dim}`);
}
}
}
/**
* Create sparse tensor representation using StateVector
*/
function createSparseTensorRepresentation(stateVector) {
// StateVector already provides efficient sparse storage
// Filter out near-zero coefficients for true sparsity
const threshold = 1e-10;
const amplitudes = stateVector.getAmplitudes();
// Create new amplitudes with true zeros for sparse elements
const sparseAmplitudes = amplitudes.map(amp => {
const magnitude = math.abs(amp);
return magnitude < threshold ? math.complex(0, 0) : amp;
});
return new stateVector_1.StateVector(stateVector.dimension, sparseAmplitudes, `sparse(${stateVector.basis || 'intertwiner'})`, {
...stateVector.properties,
sparse: true,
threshold
});
}
/**
* Extract dimensions from StateVector properties
*/
function extractDimensions(stateVector) {
const properties = stateVector.properties;
if (properties?.dimensions) {
return properties.dimensions;
}
// Fallback: assume single dimension
return [stateVector.dimension];
}
//# sourceMappingURL=tensor.js.map