ts-quantum
Version:
TypeScript library for quantum mechanics calculations and utilities
224 lines • 8.09 kB
JavaScript
"use strict";
/**
* Specialized quantum operator implementations for performance optimization
*/
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.isDiagonalMatrix = exports.createDiagonalOperator = exports.createIdentityOperator = exports.DiagonalOperator = exports.IdentityOperator = void 0;
const stateVector_1 = require("../states/stateVector");
const validation_1 = require("../utils/validation");
const operator_1 = require("./operator");
const math = __importStar(require("mathjs"));
/**
* Identity operator with no matrix storage - optimal performance
*/
class IdentityOperator {
constructor(dimension) {
this.objectType = 'operator';
this.type = 'identity';
if (dimension <= 0 || !Number.isInteger(dimension)) {
throw new Error('Dimension must be a positive integer');
}
this.dimension = dimension;
}
/**
* Apply identity operator - returns cloned state
*/
apply(state) {
(0, validation_1.validateMatchDims)(state.dimension, this.dimension);
return new stateVector_1.StateVector(state.dimension, [...state.amplitudes], state.basis);
}
/**
* Compose with another operator - returns other operator
*/
compose(other) {
(0, validation_1.validateMatchDims)(other.dimension, this.dimension);
return other;
}
/**
* Adjoint of identity is identity
*/
adjoint() {
return new IdentityOperator(this.dimension);
}
/**
* Generate identity matrix on demand
*/
toMatrix() {
return Array(this.dimension).fill(null)
.map((_, i) => Array(this.dimension).fill(null)
.map((_, j) => i === j ? math.complex(1, 0) : math.complex(0, 0)));
}
tensorProduct(other) {
const { MatrixOperator } = require('./operator');
return new MatrixOperator(this.toMatrix()).tensorProduct(other);
}
partialTrace(dims, traceOutIndices) {
// const { MatrixOperator } = require('./operator');
return new operator_1.MatrixOperator(this.toMatrix()).partialTrace(dims, traceOutIndices);
}
scale(scalar) {
const diagonal = Array(this.dimension).fill(scalar);
return new DiagonalOperator(diagonal);
}
add(other) {
const { MatrixOperator } = require('./operator');
return new MatrixOperator(this.toMatrix()).add(other);
}
eigenDecompose() {
const { MatrixOperator } = require('./operator');
return new MatrixOperator(this.toMatrix()).eigenDecompose();
}
norm() {
return Math.sqrt(this.dimension);
}
isZero() {
return false;
}
}
exports.IdentityOperator = IdentityOperator;
/**
* Diagonal operator with compressed storage
*/
class DiagonalOperator {
constructor(diagonal) {
this.objectType = 'operator';
this.type = 'diagonal';
if (diagonal.length === 0) {
throw new Error('Diagonal cannot be empty');
}
this.dimension = diagonal.length;
this.diagonal = diagonal.map(val => math.clone(val));
}
/**
* Apply diagonal operator - element-wise multiplication
*/
apply(state) {
(0, validation_1.validateMatchDims)(state.dimension, this.dimension);
const newAmplitudes = state.amplitudes.map((amp, i) => math.multiply(amp, this.diagonal[i]));
return new stateVector_1.StateVector(this.dimension, newAmplitudes, state.basis);
}
/**
* Compose with another operator
*/
compose(other) {
(0, validation_1.validateMatchDims)(other.dimension, this.dimension);
if (other.type === 'diagonal') {
const otherDiagonal = other.getDiagonal();
const resultDiagonal = this.diagonal.map((val, i) => math.multiply(val, otherDiagonal[i]));
return new DiagonalOperator(resultDiagonal);
}
const { MatrixOperator } = require('./operator');
return new MatrixOperator(this.toMatrix()).compose(other);
}
/**
* Adjoint of diagonal operator
*/
adjoint() {
const conjugateDiagonal = this.diagonal.map(val => math.conj(val));
return new DiagonalOperator(conjugateDiagonal);
}
/**
* Generate full matrix on demand
*/
toMatrix() {
return Array(this.dimension).fill(null)
.map((_, i) => Array(this.dimension).fill(null)
.map((_, j) => i === j ? math.clone(this.diagonal[i]) : math.complex(0, 0)));
}
tensorProduct(other) {
const { MatrixOperator } = require('./operator');
return new MatrixOperator(this.toMatrix()).tensorProduct(other);
}
partialTrace(dims, traceOutIndices) {
const { MatrixOperator } = require('./operator');
return new MatrixOperator(this.toMatrix()).partialTrace(dims, traceOutIndices);
}
scale(scalar) {
const scaledDiagonal = this.diagonal.map(val => math.multiply(val, scalar));
return new DiagonalOperator(scaledDiagonal);
}
add(other) {
if (other.type === 'diagonal') {
const otherDiagonal = other.getDiagonal();
const sumDiagonal = this.diagonal.map((val, i) => math.add(val, otherDiagonal[i]));
return new DiagonalOperator(sumDiagonal);
}
const { MatrixOperator } = require('./operator');
return new MatrixOperator(this.toMatrix()).add(other);
}
eigenDecompose() {
const { MatrixOperator } = require('./operator');
return new MatrixOperator(this.toMatrix()).eigenDecompose();
}
norm() {
return Math.sqrt(this.diagonal.reduce((sum, val) => {
const magnitude = Number(math.abs(val));
return sum + magnitude * magnitude;
}, 0));
}
isZero(tolerance = 1e-12) {
return this.diagonal.every(val => Math.abs(val.re) < tolerance && Math.abs(val.im) < tolerance);
}
/**
* Get diagonal elements
*/
getDiagonal() {
return this.diagonal.map(val => math.clone(val));
}
}
exports.DiagonalOperator = DiagonalOperator;
/**
* Factory function to create identity operator
*/
function createIdentityOperator(dimension) {
return new IdentityOperator(dimension);
}
exports.createIdentityOperator = createIdentityOperator;
/**
* Factory function to create diagonal operator
*/
function createDiagonalOperator(diagonal) {
return new DiagonalOperator(diagonal);
}
exports.createDiagonalOperator = createDiagonalOperator;
/**
* Check if matrix is diagonal
*/
function isDiagonalMatrix(matrix, tolerance = 1e-12) {
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (i !== j) {
const element = matrix[i][j];
if (Math.abs(element.re) > tolerance || Math.abs(element.im) > tolerance) {
return false;
}
}
}
}
return true;
}
exports.isDiagonalMatrix = isDiagonalMatrix;
//# sourceMappingURL=specialized.js.map