Skip to content

Commit

Permalink
Add option to generate either an ASCII or a STIM circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
giangiac committed Mar 13, 2024
1 parent dfc483a commit 05784ad
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
72 changes: 69 additions & 3 deletions tutorial/src/workspace/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,67 @@ export function createCircuitAsciiArt(data_qubits, anc_qubit, with_time=true, fo



/**
* Create the circuit as stim code snippet.
*
* For simplicity, a single form of circuits is available:
*
* [] "univ"
* - Hadamard on the ancilla qubit
* - both CNOT and CZ as 2q gates
* - every CNOT/CZ is controlled by the ancilla and targets a data qubit
*/
export function createCircuitStimCode(data_qubits, anc_qubit) {
let lines = [];
// Qubit coordinates.
let q = 1;
let line = '';
data_qubits.forEach(qubit => {
line = 'QUBIT_COORDS' + qubit.name.slice(1) + ` ${q}`;
q += 1;
lines.push(line);
});
const id_ancilla = `${data_qubits.length+1}`;
line = 'QUBIT_COORDS' + anc_qubit.name.slice(1) + ' ' + id_ancilla;
lines.push(line)
// In the first time step, reset the ancilla qubit.
line = 'R ' + id_ancilla;
lines.push(line);
lines.push('TICK')
// In the second time step, apply the Hadamard to the ancilla qubit.
line = 'H ' + id_ancilla;
lines.push(line);
lines.push('TICK')
// Then apply one CNOt or CZ at a time, everyone controlled by the ancilla and acting on a different data qubit.
q = 1;
data_qubits.forEach(qubit => {
if (qubit.role === QUBIT_ROLES.XDATA) line = 'CX ';
if (qubit.role === QUBIT_ROLES.ZDATA) line = 'CZ ';
line += id_ancilla + ` ${q}`;
q += 1;
lines.push(line);
lines.push('TICK')
});
// Finally, apply the Hadamard to the ancilla qubit and measure it.
line = 'H ' + id_ancilla;
lines.push(line);
lines.push('TICK')
line = 'MR ' + id_ancilla;
lines.push(line);
lines.push('TICK')
// We do not add detectors at this stage.

// Create the message
let stim = '';
lines.slice(0,-1).forEach(line => {
stim = stim + line + '\n';
});
stim += lines[lines.length-1];
return stim;
}



/**
* Circuit class
* @extends Container
Expand Down Expand Up @@ -175,13 +236,18 @@ export default class Circuit extends Container {
* Create the circuit as ASCII art
*/
_createCircuit(message) {
if (message === '') {
if (message === '' || message === 'ascii' || message === 'stim') {
const warning = 'Plaquette is not compliant.\n\n'
+ 'Requirements:\n'
+ '- there is a unique ancilla qubit\n'
+ '- all other qubits are associated with either X- or Z-stabilizers\n'
+ '- all data qubits are assumed physically connected to the ancilla qubit';
message = (this.isCompatible) ? createCircuitAsciiArt(this.data_qubits, this.anc_qubit, true, 'univ') : warning;
+ '- all data qubits are assumed physically connected to the ancilla qubit';
if (this.isCompatible === false) {
message = warning;
} else {
message = (message === 'ascii') ? createCircuitAsciiArt(this.data_qubits, this.anc_qubit, true, 'univ')
: createCircuitStimCode(this.data_qubits, this.anc_qubit);
}
}
// Create the graphics
const artText = new Text(message,
Expand Down
22 changes: 17 additions & 5 deletions tutorial/src/workspace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { button } from './button'
import Plaquette from './plaquette'
import Circuit from './circuit'

//import addListenersToTabButtons from './addListener'
//import addListenersToTabButtons from './TEMP_addListener'

/////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -140,13 +140,25 @@ export default function TqecApp() {
/////////////////////////////////////////////////////////////

// Create a button for printing the plaquette's circuit
const printCircuitButton = button('Print circuit', gridSize, 4*gridSize, 'white', 'black');
workspace.addChild(printCircuitButton);
const printAsciiCircuitButton = button('ASCII circuit', gridSize, 4*gridSize, 'white', 'black');
workspace.addChild(printAsciiCircuitButton);
const printStimCircuitButton = button(' STIM circuit', 3.5*gridSize, 4*gridSize, 'white', 'black');
workspace.addChild(printStimCircuitButton);
let circuitArt = null;
const circuitarea = document.getElementById('editableText');

printCircuitButton.on('click', (_e) => {
circuitArt = new Circuit(selectedQubits, gridSize, 5*gridSize, libraryColors[savedPlaquettes.length-1]);
printAsciiCircuitButton.on('click', (_e) => {
workspace.removeChild(circuitArt)
circuitArt = new Circuit(selectedQubits, gridSize, 5*gridSize, libraryColors[savedPlaquettes.length-1], 'ascii');
circuitArt.visible = true;
workspace.addChild(circuitArt);
let message = circuitArt.art.text;
circuitarea.value = message;
});

printStimCircuitButton.on('click', (_e) => {
workspace.removeChild(circuitArt)
circuitArt = new Circuit(selectedQubits, gridSize, 5*gridSize, libraryColors[savedPlaquettes.length-1], 'stim');
circuitArt.visible = true;
workspace.addChild(circuitArt);
let message = circuitArt.art.text;
Expand Down

0 comments on commit 05784ad

Please sign in to comment.