eaglelandsonce's picture
Update index.html
5e9bec3 verified
<!doctype html>
<html>
<head>
<title>Time Evolution of a Qubit Under a Z Hamiltonian</title>
</head>
<body>
<h5>Time Evolution of a Qubit Under a Z Hamiltonian</h5>
<button type="button" id="run-time-evolution">Run Time Evolution</button>
<br />
<br />
<div id="drawing"></div>
<pre><code id="output"></code></pre>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="https://unpkg.com/quantum-circuit"></script>
<script>
var circuit = new QuantumCircuit();
function runTimeEvolution() {
clearLog();
circuit.init();
// Define the time evolution under a Z Hamiltonian
// H = Z, U = exp(-i * H * t) = exp(-i * Z * t)
const time = Math.PI / 4; // Example time value
// Apply initial Hadamard gate to create superposition
circuit.addGate("h", 0, 0);
// Apply time evolution operator (Rz gate)
circuit.addGate("rz", 1, 0, { params: { phi: -2 * time } });
// Simulate the circuit
circuit.run();
// Log the results to the output
LogOutput();
ShowCircuit();
// Print results to the screen
const outputElement = document.getElementById("output");
outputElement.innerText = circuit.stateAsString(true);
}
function clearLog() {
document.getElementById("output").innerText = "";
}
function Log(s) {
document.getElementById("output").innerText += "\n" + s;
}
function LogOutput() {
Log("Final state:");
Log("------------");
Log(circuit.stateAsString(true));
Log("");
}
function ShowCircuit() {
var drawing = document.getElementById("drawing");
drawing.innerHTML = circuit.exportSVG(true);
}
// Ensure the button click event is properly connected
$(document).ready(function () {
$("#run-time-evolution").off("click").on("click", function () {
runTimeEvolution();
});
});
</script>
</body>
</html>