Circuit Block
Explaining the structure of a quantum circuit in comments.
Circuit block is a feature that makes it easier to understand the structure of a quantum circuit by adding comments to parts of the circuit. Circuit blocks are handy when a quantum circuit can be clearly and logically divided into several regions. A circuit block can be defined as a block of several circuit steps. The block can be commented on to explain how it works.
JavaScript API
To add a circuit block to a quantum circuit, use the block()
method.
The first argument is the comment string to be added to the block, and the second argument is the quantum circuit definition in the block, passed as a function.
<quantum-circuit id="entangled-qubits"></quantum-circuit>
<script>
window.addEventListener('DOMContentLoaded', () => {
circuit = document.getElementById("entangled-qubits")
circuit.write(0, 0, 1)
.block("entangle", (block) => { block.h(0).cnot(0, 1) })
.measure(0).measure(1)
})
</script>
If you want to insert a space between circuit blocks, you can use the i()
method to insert an empty step as follows:
<quantum-circuit id="root-of-not" class="inline-block"></quantum-circuit>
<script>
window.addEventListener('DOMContentLoaded', () => {
circuit = document.getElementById("root-of-not")
circuit.write(0, 0)
.block("RNOT", (block) => { block.h(0).phase("-π/2", 0).h(0) })
.i()
.block("RNOT", (block) => { block.h(0).phase("-π/2", 0).h(0) })
.i()
.block("Built-in RNOT", (block) => { block.rnot(0).rnot(0) })
.i()
})
</script>
JSON API
In the JSON API, the step to specify the start of a circuit block is ["{comment"]
, where comment
can be any string.
To close the circuit block, specify the step ["}"]
.
<quantum-circuit data-json="{"cols":[["|0>","|0>"],["{entangle"],["H",1],["•","X"],["}"],["Measure",1],[1,"Measure"]],"init":false}">
</quantum-circuit>
Raw HTML
To specify a circuit block in raw HTML, place the part you want to display in a block between <circuit-block> ... </circuit-block>
.
The comment string can be specified with the data-comment
attribute.
<quantum-circuit>
<circuit-step>
<circuit-dropzone>
<write-gate></write-gate>
</circuit-dropzone>
<circuit-dropzone>
<write-gate></write-gate>
</circuit-dropzone>
</circuit-step>
<circuit-block data-comment="entangle">
<circuit-step>
<circuit-dropzone>
<h-gate></h-gate>
</circuit-dropzone>
<circuit-dropzone>
</circuit-dropzone>
</circuit-step>
<circuit-step>
<circuit-dropzone>
<control-gate></control-gate>
</circuit-dropzone>
<circuit-dropzone>
<x-gate></x-gate>
</circuit-dropzone>
</circuit-step>
</circuit-block>
<circuit-step>
<circuit-dropzone>
<measurement-gate></measurement-gate>
</circuit-dropzone>
<circuit-dropzone>
</circuit-dropzone>
</circuit-step>
<circuit-step>
<circuit-dropzone>
</circuit-dropzone>
<circuit-dropzone>
<measurement-gate></measurement-gate>
</circuit-dropzone>
</circuit-step>
</quantum-circuit>