ts-quantum
Version:
TypeScript library for quantum mechanics calculations and utilities
155 lines • 4.88 kB
JavaScript
/**
* Core QuantumGraph implementation
*/
import { CompositeQuantumManager } from './CompositeQuantumManager';
import { GraphologyAdapter } from '../../../graph-core/src/core/GraphologyAdapter';
import { applyQuantumOperation, partialMeasurement } from './operations/general';
/**
* Implementation of quantum-labeled graph
*/
export class QuantumGraph {
adapter;
quantumNodes = new Map();
quantumEdges = new Map();
compositeManager;
constructor(baseGraph) {
this.adapter = baseGraph || new GraphologyAdapter();
this.compositeManager = new CompositeQuantumManager();
}
// Quantum labeling methods
setVertexQuantumObject(nodeId, obj) {
if (!this.adapter.hasNode(nodeId)) {
throw new Error(`Node ${nodeId} does not exist in graph`);
}
this.quantumNodes.set(nodeId, obj);
}
getVertexQuantumObject(nodeId) {
// Check composite first, fallback to individual
return this.compositeManager.getCompositeForElement(nodeId) || this.quantumNodes.get(nodeId);
}
setEdgeQuantumObject(edgeId, obj) {
if (!this.adapter.hasEdge(edgeId)) {
throw new Error(`Edge ${edgeId} does not exist in graph`);
}
this.quantumEdges.set(edgeId, obj);
}
getEdgeQuantumObject(edgeId) {
// Check composite first, fallback to individual
return this.compositeManager.getCompositeForElement(edgeId) || this.quantumEdges.get(edgeId);
}
// Utility methods
hasVertexQuantumObject(nodeId) {
return this.quantumNodes.has(nodeId);
}
hasEdgeQuantumObject(edgeId) {
return this.quantumEdges.has(edgeId);
}
clearVertexQuantumObject(nodeId) {
this.quantumNodes.delete(nodeId);
}
clearEdgeQuantumObject(edgeId) {
this.quantumEdges.delete(edgeId);
}
// Composite quantum object management
setCompositeQuantumObject(elementIds, obj) {
this.compositeManager.setComposite(elementIds, obj);
}
getCompositeQuantumObject(elementIds) {
return this.compositeManager.getComposite(elementIds);
}
// General quantum operations
applyVertexOperation(vertexIds, operator) {
applyQuantumOperation(this, vertexIds, operator);
}
applyEdgeOperation(edgeIds, operator) {
applyQuantumOperation(this, edgeIds, operator);
}
applyOperation(elementIds, operator) {
applyQuantumOperation(this, elementIds, operator);
}
measureSubsystem(vertexIds, projector) {
return partialMeasurement(this, vertexIds, projector);
}
// Access to underlying graph adapter
getGraphAdapter() {
return this.adapter;
}
// Delegate IGraph methods to adapter
get isDirected() {
return this.adapter.isDirected;
}
get nodeCount() {
return this.adapter.nodeCount;
}
get edgeCount() {
return this.adapter.edgeCount;
}
addNode(node) {
return this.adapter.addNode(node);
}
removeNode(nodeId) {
this.quantumNodes.delete(nodeId);
return this.adapter.removeNode(nodeId);
}
addEdge(edge) {
return this.adapter.addEdge(edge);
}
removeEdge(edgeId) {
this.quantumEdges.delete(edgeId);
return this.adapter.removeEdge(edgeId);
}
getNode(nodeId) {
return this.adapter.getNode(nodeId);
}
getEdge(edgeId) {
return this.adapter.getEdge(edgeId);
}
getNodes() {
return this.adapter.getNodes();
}
getEdges() {
return this.adapter.getEdges();
}
getAdjacentNodes(nodeId, options) {
return this.adapter.getAdjacentNodes(nodeId, options);
}
getConnectedEdges(nodeId, options) {
return this.adapter.getConnectedEdges(nodeId, options);
}
findPath(fromId, toId, options) {
return this.adapter.findPath(fromId, toId, options);
}
toAdjacencyMatrix(weightFn) {
return this.adapter.toAdjacencyMatrix(weightFn);
}
toLaplacianMatrix(weightFn) {
return this.adapter.toLaplacianMatrix(weightFn);
}
setMetadata(metadata) {
return this.adapter.setMetadata(metadata);
}
getMetadata() {
return this.adapter.getMetadata();
}
hasNode(nodeId) {
return this.adapter.hasNode(nodeId);
}
hasEdge(edgeId) {
return this.adapter.hasEdge(edgeId);
}
areNodesAdjacent(sourceId, targetId, options) {
return this.adapter.areNodesAdjacent(sourceId, targetId, options);
}
getNodeDegree(nodeId, options) {
return this.adapter.getNodeDegree(nodeId, options);
}
clone() {
return this.adapter.clone();
}
clear() {
this.quantumNodes.clear();
this.quantumEdges.clear();
return this.adapter.clear();
}
}
//# sourceMappingURL=QuantumGraph.js.map