quantum-circuit
quantum-circuit copied to clipboard
Interactive measurements: not clear if possible/how to do
Hello,
First thanks for this promising package. I'm trying to implement an interactive circuit, in which the user can say in real time the operations to do depending on the previous measurements outcome (NB: it's different from conditional gate as it is really interactive):
- please measure qubit 0.
- The result is 1. What do you want to do?
- Oh, I'd like to see, what happens now if I measure qubit 1 ?
- The result is 1.
I tried to use the measure operation to implement a basic measurement of a bell pair (expect 00 or 11 outcome), but does not work as I expect (always 00 outcome, no 11), so I guess I missed something. Is it a bug, or did I missed something?
I saw that there is the measure gate, but I'm affraid by the warning Measurement gate will reset qubit to measured value only if there are gates with classical control, as here, I really want to collapse the state after the first measurement, otherwise I'll get inconsistent measurements.
<!doctype html>
<html>
<head>
<title>Measure bell state </title>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript" src="https://unpkg.com/quantum-circuit"></script>
<script type="text/javascript">
for(let i=0; i < 10; i++) { // Repeat the circuit 10 times to see if the probability is the expected one: Pb: only 00 outcome, no 11
console.log("=== New game: expect sometimes outcome 00, sometimes 11");
circuit = new QuantumCircuit();
circuit.addGate("h", -1, [0]);
circuit.addGate("cx", -1, [0, 1]);
console.log("Alice will measure")
console.log(circuit.measure(0))
console.log("Now bob will measure")
console.log(circuit.measure(1))
}
</script>
</body>
</html>
@tobiasBora you need to put circuit.run() before first measurement. addGate realy only adds gate to the circuit, doesn't execute the gate.
<!doctype html>
<html>
<head>
<title>Measure bell state </title>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript" src="https://unpkg.com/quantum-circuit"></script>
<script type="text/javascript">
circuit = new QuantumCircuit();
circuit.addGate("h", -1, [0]);
circuit.addGate("cx", -1, [0, 1]);
for(let i=0; i < 10; i++) {
//
// Run the simulation
//
circuit.run();
console.log("=== New game: expect sometimes outcome 00, sometimes 11");
console.log("Alice will measure")
console.log(circuit.measure(0))
console.log("Now bob will measure")
console.log(circuit.measure(1))
}
</script>
</body>
</html>
P.S. you can use measureAllMultishot(shots) as well:
<!doctype html>
<html>
<head>
<title>Measure bell state </title>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript" src="https://unpkg.com/quantum-circuit"></script>
<script type="text/javascript">
circuit = new QuantumCircuit();
circuit.addGate("h", -1, [0]);
circuit.addGate("cx", -1, [0, 1]);
//
// Run the simulation
//
circuit.run();
//
// Multishot measurement
//
console.log(circuit.measureAllMultishot(10));
</script>
</body>
</html>
One more thing: when you do measure() for the first time (after .run()) it internally calls measureAll() which caches measurement results. Any call to measure() after that will give you the same result (if you don't run() circuit again). That is actually also how real device behaves - measurement destroys the superposition, any subsequent measurement will return the same result unless you execute the program again.
If circuit is big (many qubits) then .run() takes a lot of time. In that case you can reset cache by executing mesureAll(true) before your measure(qubit) operations.
Example:
<!doctype html>
<html>
<head>
<title>Measure bell state </title>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript" src="https://unpkg.com/quantum-circuit"></script>
<script type="text/javascript">
circuit = new QuantumCircuit();
circuit.addGate("h", -1, [0]);
circuit.addGate("cx", -1, [0, 1]);
//
// Run the simulation
//
circuit.run();
for(let i=0; i < 10; i++) {
//
// Reset measurement cache
//
circuit.measureAll(true);
console.log("=== New game: expect sometimes outcome 00, sometimes 11");
console.log("Alice will measure")
console.log(circuit.measure(0))
console.log("Now bob will measure")
console.log(circuit.measure(1))
}
</script>
</body>
</html>
Thanks for your answer, however it does not really solve my need. Indeed, by "adaptive measurements", I mean that I'd like to be able to perform new gates after the measurement done by Alice (I really want a real user to say what measurement he want to do, and I can't know the measurement in advance). I tried the following (that just add a X on Bob's qubit), but I get wrong results (like 11 which is supposed to be impossible, the only outcomes behing 01 and 10).
<!doctype html>
<html>
<head>
<title>Measure bell state </title>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript" src="https://unpkg.com/quantum-circuit"></script>
<script type="text/javascript">
circuit = new QuantumCircuit();
circuit.addGate("h", -1, [0]);
circuit.addGate("cx", -1, [0, 1]);
for(let i=0; i < 10; i++) {
//
// Run the simulation
//
circuit.run();
console.log("=== New game: expect sometimes outcome 01, sometimes 10");
console.log("Alice will measure")
console.log(circuit.measure(0))
console.log("Now bob will measure")
// I want to perform operations *after* alice measured her state
circuit.addGate("x", -1, [1]);
console.log(circuit.measure(1))
}
</script>
</body>
</html>
When you add a gate, state is not changed. Remember: addGate adds a gate to the circuit but doesn't execute a gate.
You need to do run() after modifying circuit (but that will destroy measurement cache).
Let's see... maybe instead addGate(...) you can do:
applyGate(gateName, column, wires, options)
But that will also reset measurement cache.
Hum... the best would be to run circuit with circuit.run(null, { strictMode: true }) that will destroy superposition after measurement (like real device), and then use applyGate (instead addGate) for additional gates (added on the fly)
Note:
strictMode: true is not properly tested, so be careful.
Your code would look something like:
<!doctype html>
<html>
<head>
<title>Measure bell state </title>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript" src="https://unpkg.com/quantum-circuit"></script>
<script type="text/javascript">
circuit = new QuantumCircuit();
circuit.addGate("h", -1, [0]);
circuit.addGate("cx", -1, [0, 1]);
for(let i=0; i < 10; i++) {
//
// Run the simulation
//
circuit.run(null, { strictMode: true });
console.log("=== New game: expect sometimes outcome 01, sometimes 10");
console.log("Alice will measure")
console.log(circuit.measure(0))
//
// Now both qubit's superposition is destroyed because we use strictMode (like a real device), so if alice measured |1>, both qubits are now |1> (because qubits are maximally entangled).
//
console.log("Now bob will measure")
//
// Execute gate "on the fly". If Alice measured 1 then Bob's qubit is 1 as well, so this will set his qubit to 0
//
circuit.applyGate("x", -1, [1]);
console.log(circuit.measure(1))
}
</script>
</body>
</html>
I just tested - this will not work. Sorry, what you are trying to do is not implemented (adding gates on the fly while preserving state)
However, why you don't do this:
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
cx q[0], q[1];
measure q[0] -> c[0];
x q[1];
measure q[1] -> c[1];

Or in javascript:
const circuit = new QuantumCircuit(2);
circuit.createCreg("c", 2);
circuit.appendGate("h", 0, {});
circuit.appendGate("cx", [0,1], {});
circuit.addMeasure(0, "c", 0);
circuit.appendGate("x", 1, {});
circuit.addMeasure(1, "c", 1);
circuit.run();
That will set classical registers to 01 or 10.
There is also onGate callback in options to run:
circuit.run(null, { onGate: function(column, wire, gateCounter) { /* print whatever */ } });
Ok, thanks for the answer, too bad it's not implemented. Let me know if there is any plan for that.
And concerning your last proposition I'm not sure to understand, how does it help to apply gates on the fly?