ts-quantum
Version:
TypeScript library for quantum mechanics calculations and utilities
141 lines • 5.33 kB
JavaScript
;
/**
* Measurement operations for quantum states
*/
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.createMeasurementOperator = exports.measureState = exports.expectationValue = exports.ProjectionOperator = void 0;
const operator_1 = require("./operator");
const stateVector_1 = require("../states/stateVector");
const math = __importStar(require("mathjs"));
/**
* Implementation of a projection operator for quantum measurements
*/
class ProjectionOperator {
constructor(state) {
this._dimension = state.dimension;
// Create projection matrix |ψ⟩⟨ψ| with proper complex number initialization
const matrix = Array(state.dimension).fill(null)
.map(() => Array(state.dimension).fill(null).map(() => math.complex(0, 0)));
for (let i = 0; i < state.dimension; i++) {
for (let j = 0; j < state.dimension; j++) {
// |ψ⟩⟨ψ| = ψi * ψj*
matrix[i][j] = math.multiply(math.complex(state.amplitudes[i].re, state.amplitudes[i].im), math.conj(state.amplitudes[j]));
}
}
// Create operator without validation since we know it's a valid projection
this._operator = new operator_1.MatrixOperator(matrix, 'projection', false);
}
get objectType() {
return 'operator';
}
get dimension() {
return this._dimension;
}
get type() {
return 'projection';
}
/**
* Tests whether the density matrix is identically zero
*/
isZero(tolerance) {
return this._operator.isZero(tolerance);
}
norm() {
return this._operator.norm();
}
apply(state) {
return this._operator.apply(state);
}
compose(other) {
return this._operator.compose(other);
}
adjoint() {
// Create new MatrixOperator since projection operators are Hermitian
return new operator_1.MatrixOperator(this.toMatrix(), 'projection');
}
toMatrix() {
return this._operator.toMatrix();
}
tensorProduct(other) {
return this._operator.tensorProduct(other);
}
partialTrace(dims, traceOutIndices) {
return this._operator.partialTrace(dims, traceOutIndices);
}
scale(scalar) {
return this._operator.scale(scalar);
}
add(other) {
return this._operator.add(other);
}
eigenDecompose() {
return this._operator.eigenDecompose();
}
}
exports.ProjectionOperator = ProjectionOperator;
/**
* Calculate expectation value of an operator for a given state
*/
function expectationValue(state, operator) {
const resultState = operator.apply(state);
let result = math.complex(0, 0);
for (let i = 0; i < state.dimension; i++) {
// ⟨ψ|A|ψ⟩ = Σ ψi* (A|ψ⟩)i
result = math.add(result, math.multiply(math.conj(state.amplitudes[i]), resultState.amplitudes[i]));
}
return result;
}
exports.expectationValue = expectationValue;
/**
* Perform a measurement on a quantum state with a given observable
*/
function measureState(state, operator) {
// For a projective measurement, the eigenvalue is 1 for the measured state
const eigenvalue = 1;
// Apply measurement operator
const resultState = operator.apply(state);
// Calculate probability from norm squared of resulting state
const probability = resultState.amplitudes.reduce((sum, amp) => sum + math.abs(amp) ** 2, 0);
// Normalize the post-measurement state
const normalizedAmplitudes = resultState.amplitudes.map(amp => math.divide(amp, math.sqrt(probability)));
// Create new StateVector instance
return {
value: eigenvalue,
probability,
state: new stateVector_1.StateVector(state.dimension, normalizedAmplitudes, state.basis)
};
}
exports.measureState = measureState;
/**
* Create a measurement operator for a given observable and eigenvalue
*/
function createMeasurementOperator(observable, eigenvalue) {
// This would involve eigendecomposition of the observable
// For now, we'll just implement projection measurements
throw new Error('General measurement operators not yet implemented');
}
exports.createMeasurementOperator = createMeasurementOperator;
//# sourceMappingURL=measurement.js.map