quaco.js
Version:
A lightweight modular Quantum Computing Simulator in JavaScript. Supports qubits, quantum gates, entanglement, circuits, algorithms, and visualization.
101 lines (91 loc) • 2.55 kB
JavaScript
// src/QuantumGate.js
import { Complex } from './utils/Complex.js';
/**
* Static class providing common quantum gates.
*/
export class QuantumGate {
/**
* Identity Gate (I)
* Leaves the qubit unchanged.
*/
static identity() {
return [
[new Complex(1, 0), new Complex(0, 0)],
[new Complex(0, 0), new Complex(1, 0)]
];
}
/**
* Pauli-X Gate (NOT gate)
* Flips |0⟩ to |1⟩ and vice-versa.
*/
static x() {
return [
[new Complex(0, 0), new Complex(1, 0)],
[new Complex(1, 0), new Complex(0, 0)]
];
}
/**
* Pauli-Y Gate
* Applies a phase flip and bit flip.
*/
static y() {
return [
[new Complex(0, 0), new Complex(0, -1)],
[new Complex(0, 1), new Complex(0, 0)]
];
}
/**
* Pauli-Z Gate
* Applies a phase flip to |1⟩ state.
*/
static z() {
return [
[new Complex(1, 0), new Complex(0, 0)],
[new Complex(0, 0), new Complex(-1, 0)]
];
}
/**
* Hadamard Gate (H)
* Creates superposition.
*/
static hadamard() {
const factor = 1 / Math.sqrt(2);
return [
[new Complex(factor, 0), new Complex(factor, 0)],
[new Complex(factor, 0), new Complex(-factor, 0)]
];
}
/**
* Phase Gate (S Gate)
* Adds a π/2 phase to |1⟩.
*/
static s() {
return [
[new Complex(1, 0), new Complex(0, 0)],
[new Complex(0, 0), new Complex(0, 1)]
];
}
/**
* T Gate (π/4 phase gate)
*/
static t() {
const sqrtHalf = Math.sqrt(0.5);
return [
[new Complex(1, 0), new Complex(0, 0)],
[new Complex(0, 0), new Complex(sqrtHalf, sqrtHalf)]
];
}
/**
* Controlled-NOT Gate (CNOT)
* 2-qubit gate flipping target qubit if control is |1⟩.
* Returns a 4x4 matrix.
*/
static cnot() {
return [
[new Complex(1, 0), new Complex(0, 0), new Complex(0, 0), new Complex(0, 0)],
[new Complex(0, 0), new Complex(1, 0), new Complex(0, 0), new Complex(0, 0)],
[new Complex(0, 0), new Complex(0, 0), new Complex(0, 0), new Complex(1, 0)],
[new Complex(0, 0), new Complex(0, 0), new Complex(1, 0), new Complex(0, 0)],
];
}
}