{"id":13536086,"url":"https://github.com/quantastica/quantum-circuit","last_synced_at":"2026-01-16T14:14:26.821Z","repository":{"id":36607014,"uuid":"74958916","full_name":"quantastica/quantum-circuit","owner":"quantastica","description":"Quantum Circuit Simulator implemented in JavaScript","archived":false,"fork":false,"pushed_at":"2024-08-04T00:40:43.000Z","size":166915,"stargazers_count":248,"open_issues_count":12,"forks_count":49,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-10-09T21:09:19.207Z","etag":null,"topics":["cirq","emulator","pyquil","qasm","qiskit","quantum","quantum-circuit","quantum-computing","qubit","qubits","quil","simulator"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/quantastica.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-28T09:48:47.000Z","updated_at":"2024-10-03T08:29:08.000Z","dependencies_parsed_at":"2023-01-17T03:04:03.822Z","dependency_job_id":"d8c6209b-6efb-4e5c-9c85-8e3b4556ed92","html_url":"https://github.com/quantastica/quantum-circuit","commit_stats":{"total_commits":477,"total_committers":5,"mean_commits":95.4,"dds":"0.13626834381551367","last_synced_commit":"8ad0641b43fe5a231a863efcda1a7c25a1c7320c"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantastica%2Fquantum-circuit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantastica%2Fquantum-circuit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantastica%2Fquantum-circuit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantastica%2Fquantum-circuit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quantastica","download_url":"https://codeload.github.com/quantastica/quantum-circuit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246743887,"owners_count":20826626,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cirq","emulator","pyquil","qasm","qiskit","quantum","quantum-circuit","quantum-computing","qubit","qubits","quil","simulator"],"created_at":"2024-08-01T09:00:34.546Z","updated_at":"2026-01-16T14:14:26.814Z","avatar_url":"https://github.com/quantastica.png","language":"JavaScript","funding_links":[],"categories":["Quantum SDKs","JavaScript"],"sub_categories":[],"readme":"# Quantum Circuit Simulator\n\n[quantum-circuit](https://www.npmjs.com/package/quantum-circuit) is open source quantum circuit simulator implemented in javascript. Smoothly runs 20+ qubit simulations in browser or at server (node.js). You can use it in your javascript program to run quantum simulations. \n\nCircuit can be imported from [OpenQASM](https://github.com/Qiskit/openqasm), [Quil](https://arxiv.org/abs/1608.03355) and [IONQ](https://docs.ionq.com/). You can export circuits to [OpenQASM](https://github.com/Qiskit/openqasm), [pyQuil](http://docs.rigetti.com/en/latest/index.html), [Quil](https://arxiv.org/abs/1608.03355), [Qiskit](https://qiskit.org/documentation/), [Cirq](https://github.com/quantumlib/Cirq), [TensorFlow Quantum](https://www.tensorflow.org/quantum), [QSharp](https://docs.microsoft.com/en-us/quantum/language/index?view=qsharp-preview), and [CudaQ](https://nvidia.github.io/cuda-quantum/latest/index.html), so it can be used for conversion between quantum programming languages. Circuit drawing can be exported to [SVG](https://www.w3.org/Graphics/SVG/) vector image.\n\n\n## Live examples\n\n### Quantum Programming Studio\n\n[Quantum Programming Studio](https://quantum-circuit.com) is web based quantum programming IDE and simulator built on top of this package. Circuit can be executed on real quantum computer directly from the UI.\n\n### Other live examples\n\n- [qconvert](https://quantum-circuit.com/qconvert) Online quantum programming language converter\n\n- [example.html](https://quantum-circuit.com/example.html)\n\n\n## Using in browser\n\nSimply include [quantum-circuit.min.js](dist/) into your html page (available via unpkg CDN [https://unpkg.com/quantum-circuit](https://unpkg.com/quantum-circuit))\n\n```html\n\u003c!doctype html\u003e\n\u003chtml\u003e\n    \u003chead\u003e\n        \u003ctitle\u003eQuantum Circuit Simulator Example\u003c/title\u003e\n    \u003c/head\u003e\n\n    \u003cbody\u003e\n        \u003cscript type=\"text/javascript\" src=\"https://unpkg.com/quantum-circuit\"\u003e\u003c/script\u003e\n\n        \u003cscript type=\"text/javascript\"\u003e\n            // Your code here\n        \u003c/script\u003e\n    \u003c/body\u003e\n\u003c/html\u003e\n```\n\n\n## Using at server with node.js\n\nAdd [quantum-circuit](https://www.npmjs.com/package/quantum-circuit) npm module to your node.js project:\n\n```bash\nnpm install --save quantum-circuit\n```\n\nAnd then import it into your program:\n\n```javascript\nvar QuantumCircuit = require(\"quantum-circuit\");\n\n// Your code here\n\n```\n\n### Node.js examples\n\nSee [/example/nodejs](example/nodejs/) directory.\n\n\nUsing with Jupyter notebook\n---------------------------\n\nYou need to install [ijavascript](https://github.com/n-riesco/ijavascript) kernel for [Jupyter notebook](http://jupyter.org/)\n\nYou can install quantum-circuit npm module globally and invoke jupyter notebook from any directory:\n\n```\nnpm install -g quantum-circuit\n```\n\nOr inside new directory do:\n\n```\nnpm init\nnpm install --save quantum-circuit\njupyter notebook\n```\n\n### Jupyter notebook examples\n\nSee [/example/jupyter](example/jupyter/) directory.\n\n\n# Getting started\n\n## Create circuit\n\nCreate instance of `QuantumCircuit` class, optionally passing number of qubits (wires) to constructor:\n\n```javascript\nvar circuit = new QuantumCircuit(3);\n```\n*Note: number of qubits is optional argument - circuit will expand automatically if you add gates to non-existing wires*\n\n\n## Add single-qubit gates\n\nCall `appendGate` method passing gate name and qubit (wire) index:\n\n```javascript\ncircuit.appendGate(gateName, wire, options);\n```\n\nFor example, to add Hadamard gate to a first qubit (wire 0) type:\n\n```javascript\ncircuit.appendGate(\"h\", 0);\n```\n\nFor parametrized gates, provide `options` object with `params`:\n\n```javascript\ncircuit.appendGate(\"ry\", 0, { \"params\": { \"theta\":\"pi/2\" } });\n```\n\n**For more control on gate placement use `addGate` and specify a column as well:**\n\nCall `addGate` method passing gate name, column index and qubit (wire) index:\n\n```javascript\ncircuit.addGate(gateName, column, wire);\n```\n\nFor example, to add Hadamard to a second column (column 1) at first qubit (wire 0) type:\n\n```javascript\ncircuit.addGate(\"h\", 1, 0);\n```\n\nResult is:\n\n```\n                                \n         Column 0    Column 1   \n                               \n                     |-----|   \nWire 0 --------------|  H  |---\n                     |-----|   \n                               \n                               \n```\n\n*Note: if `column` is negative integer then gate will be added to the end of the wire*\n\n\n## Add multi-qubit gates\n\nCall `appendGate` method passing gate name and array of qubit indexes (wires):\n\n```javascript\ncircuit.appendGate(gateName, arrayOfWires);\n```\n\nExample:\n```javascript\ncircuit.appendGate(\"cx\", [0, 1]);\n```\n\nFor parametrized gates, provide `options` object with `params`:\n\n```javascript\ncircuit.appendGate(\"cry\", [0, 1], { \"params\": { \"theta\":\"pi/2\" } });\n```\n\n**For more control on gate placement use `addGate` and specify a column as well:**\n\n\nCall `addGate` method passing gate name, column index and array of connected qubits (wires):\n\n```javascript\ncircuit.addGate(gateName, column, arrayOfWires);\n```\n\nFor example, to add CNOT to a second column (column 1) controlled by second qubit (wire 1) at third qubit as target (wire 2) do:\n\n```javascript\ncircuit.addGate(\"cx\", 1, [1, 2]);\n```\n\n```\n                                \n         Column 0    Column 1   \n                               \nWire 0 ------------------------\n                               \n                               \nWire 1 -----------------o------\n                        |      \n                     |-----|   \nWire 2 --------------| CX  |---\n                     |-----|   \n                               \n```\n\n*Note: if `column` is negative integer then gate will be added to the end*\n\n\n## Example - Quantum random number generator\n\n```javascript\n\nvar QuantumCircuit = require(\"quantum-circuit\");\n\n//\n// 8-bit quantum random number generator\n//\n\nvar quantumRandom = function() {\n\n    var circuit = new QuantumCircuit();\n\n    for(var i = 0; i \u003c 8; i++) {\n        //\n        // add Hadamard gate to the end (-1) of i-th wire\n        //\n        circuit.appendGate(\"h\", i);\n\n        //\n        // add measurement gate to i-th qubit which will store result \n        // into classical register \"c\", into i-th classical bit\n        //\n        circuit.addMeasure(i, \"c\", i); \n    }\n\n    // run circuit\n    circuit.run();\n\n    // return value of register \"c\"\n    return circuit.getCregValue(\"c\");\n};\n\n// Usage - print random number to terminal\nconsole.log(quantumRandom());\n\n```\n\n\n## Implemented gates\n\n| Name | pyQuil | Cirq | Q# | IONQ | Qubits | Params | Description |\n| --- | --- | --- | --- | --- | --- | --- | --- |\n| **id** | I | I | I |  | 1 |  | Single qubit identity gate |\n| **x** | X | X | X | x | 1 |  | Pauli X (PI rotation over X-axis) aka \"NOT\" gate |\n| **y** | Y | Y | Y | y | 1 |  | Pauli Y (PI rotation over Y-axis) |\n| **z** | Z | Z | Z | z | 1 |  | Pauli Z (PI rotation over Z-axis) |\n| **h** | H | H | H | h | 1 |  | Hadamard gate |\n| **srn** | def srn | X**(1/2) |  | v | 1 |  | Square root of NOT |\n| **srndg** | def srndg | X**(-1/2) |  | vi | 1 |  | Inverse square root of NOT |\n| **r2** | S | S | S |  | 1 |  | PI/2 rotation over Z-axis aka \"Phase PI/2\" |\n| **r4** | T | T | T |  | 1 |  | PI/4 rotation over Z-axis aka \"Phase PI/4\" |\n| **r8** | PHASE(pi/8) | u1(pi/8) |  |  | 1 |  | PI/8 rotation over Z-axis aka \"Phase PI/8\" |\n| **rx** | RX | rx | Rx | rx | 1 | theta | Rotation around the X-axis by given angle |\n| **ry** | RY | ry | Ry | ry | 1 | theta | Rotation around the Y-axis by given angle |\n| **rz** | RZ | rz | Rz | rz | 1 | phi | Rotation around the Z-axis by given angle |\n| **u1** | PHASE | def u1 |  |  | 1 | lambda | Single-qubit rotation about the Z axis |\n| **u2** | def u2 | def u2 |  |  | 1 | phi, lambda | Single-qubit rotation about the X+Z axis |\n| **u3** | def u3 | def u3 |  |  | 1 | theta, phi, lambda | Generic single-qubit rotation gate with 3 Euler angles |\n| **s** | S | S | S | s | 1 |  | PI/2 rotation over Z-axis (synonym for `r2`) |\n| **t** | T | T | T | t | 1 |  | PI/4 rotation over Z-axis (synonym for `r4`) |\n| **sdg** | PHASE(-pi/2) | u1(-pi/2) |  | si | 1 |  | (-PI/2) rotation over Z-axis |\n| **tdg** | PHASE(-pi/4) | u1(-pi/4) |  | ti | 1 |  | (-PI/4) rotation over Z-axis |\n| **gpi** | def gpi | gpi |  | gpi | 1 | phi | GPi gate |\n| **gpi2** | def gpi2 | gpi2 |  | gpi2 | 1 | phi | GPi2 gate |\n| **vz** | def vz | vz |  | vz | 1 | theta | VirtualZ gate |\n| **cx** | CNOT | CNOT | CNOT | cnot | 2 |  | Controlled NOT (CNOT) gate |\n| **cy** | def cy | Y | Controlled Y |  | 2 |  | Controlled Y gate (controlled rotation over Y-axis by PI) |\n| **cz** | CZ | CZ | Controlled Z |  | 2 |  | Controlled Z gate (controlled rotation over Z-axis by PI) |\n| **ch** | def ch | H | Controlled H |  | 2 |  | Controlled Hadamard gate |\n| **csrn** | def csrn | X**(1/2) |  |  | 2 |  | Controlled square root of NOT |\n| **swap** | SWAP | SWAP | SWAP | swap | 2 |  | Swaps the state of two qubits. |\n| **srswap** | def srswap | SWAP**(1/2) |  |  | 2 |  | Square root of swap |\n| **iswap** | ISWAP | ISWAP |  |  | 2 |  | Swaps the state of two qubits, applying a -i phase to q1 when it is in the 1 state and a -i phase to q2 when it is in the 0 state |\n| **xx** | def xx | xx |  | xx | 2 | theta | XX gate |\n| **yy** | def yy | YY |  | yy | 2 | theta | YY gate |\n| **zz** | def zz |  |  | zz | 2 | theta | Parametric 2-qubit rotation about ZZ |\n| **xy** | XY | def xy |  |  | 2 | phi | XY gate |\n| **ms** | def ms | ms |  | ms | 2 | phi0, phi1 | Mølmer-Sørensen gate |\n| **cr2** | CPHASE(pi/2) | cu1(pi/2) |  |  | 2 |  | Controlled PI/2 rotation over Z-axis |\n| **cr4** | CPHASE(pi/4) | cu1(pi/4) |  |  | 2 |  | Controlled PI/4 rotation over Z-axis |\n| **cr8** | CPHASE(pi/8) | cu1(pi/8) |  |  | 2 |  | Controlled PI/8 rotation over Z-axis |\n| **crx** | def crx | rx(theta) | Controlled Rx |  | 2 | theta | Controlled rotation around the X-axis by given angle |\n| **cry** | def cry | ry(theta) | Controlled Ry |  | 2 | theta | Controlled rotation around the Y-axis by given angle |\n| **crz** | def crz | rz(phi) | Controlled Rz |  | 2 | phi | Controlled rotation around the Z-axis by given angle |\n| **cu1** | CPHASE | def cu1 |  |  | 2 | lambda | Controlled rotation about the Z axis |\n| **cu2** | def cu2 | def cu2 |  |  | 2 | phi, lambda | Controlled rotation about the X+Z axis |\n| **cu3** | def cu3 | def cu3 |  |  | 2 | theta, phi, lambda | Controlled rotation gate with 3 Euler angles |\n| **cs** | CPHASE(pi/2) | cu1(pi/2) |  |  | 2 |  | Controlled PI/2 rotation over Z-axis (synonym for `cr2`) |\n| **ct** | CPHASE(pi/4) | cu1(pi/4) |  |  | 2 |  | Controlled PI/4 rotation over Z-axis (synonym for `cr4`) |\n| **csdg** | CPHASE(-pi/2) | cu1(-pi/2) |  |  | 2 |  | Controlled (-PI/2) rotation over Z-axis |\n| **ctdg** | CPHASE(-pi/4) | cu1(-pi/4) |  |  | 2 |  | Controlled (-PI/4) rotation over Z-axis |\n| **ccx** | CCNOT | CCX | CCNOT |  | 3 |  | Toffoli aka \"CCNOT\" gate |\n| **cswap** | CSWAP | CSWAP | Controlled SWAP |  | 3 |  | Controlled swap aka \"Fredkin\" gate |\n| **csrswap** | def csrswap | SWAP**(1/2) |  |  | 3 |  | Controlled square root of swap |\n| **reset** | RESET | reset | Reset |  | 1 |  | Resets qubit |\n| **measure** | MEASURE | measure | M |  | 1 |  | Measures qubit and stores outcome (0 or 1) into classical register |\n| **barrier** | FENCE |  |  |  | 1 |  | Barrier |\n\n\n*For more details see [gate reference](#gates)*\n\n\n## Run circuit\n\nSimply call `run` method.\n\n```javascript\ncircuit.run();\n```\n\n## Initial state\n\nBy default, initial state of each qubit is `|0\u003e`. You can pass initial values as array of bool (`true` or `false`) or integers (`0` or `1`). This will set first two qubits to `|1\u003e` and evaluate circuit:\n\n```javascript\ncircuit.run([1, 1]);\n```\n\n\n## Measurement\n\nMethod `probabilities()` will return array of probabilities (real numbers between 0 and 1) for each qubit:\n\n```javascript\nconsole.log(circuit.probabilities());\n```\n\nMethod `probability(wire)` will return probability (real number between 0 and 1) for given qubit:\n\n```javascript\nconsole.log(circuit.probability(0));\n```\n\nMethod `measureAll()` returns array of chances (as integers 0 or 1) for each qubit:\n\nExample:\n```javascript\nconsole.log(circuit.measureAll());\n```\n\nMethod `measure(wire)` returns chance (as integer 0 or 1) for given qubit:\n\nExample:\n```javascript\nconsole.log(circuit.measure(0));\n```\n\nYou can store measurement into classical register. For example, to measure first qubit (wire 0) and store result into classical register named `c` as fourth bit (bit 3):\n\n```javascript\ncircuit.measure(0, \"c\", 3);\n```\n\nYou can add `measure` gate to circuit and then measurement will be done automatically and result will be stored into classical register:\n\n```javascript\ncircuit.addGate(\"measure\", -1, 0, { creg: { name: \"c\", bit: 3 } });\n```\n\nShort form of writing this is `addMeasure(wire, creg, cbit)`:\n\n```javascript\ncircuit.addMeasure(0, \"c\", 3);\n```\n\n*Note:*\n\n- *Measurement gate will reset qubit to measured value only if there are gates with classical control (gates controlled by classical registers). Otherwise, measurement gate will leave qubit as is - measured value will be written to classical register and qubit will remain unchanged. This \"nondestructive\" behavior is handy when experimenting. However, it will automatically switches to \"destructive\" mode when needed (when classical control is present)*\n\n- *If specified classical register doesn't exists - it will be created automatically.*\n\n\n## Classical registers\n\n**Create register**\n\nClassical registers are created automatically if you add measurement gate to the circuit but you can also manually create registers by calling `createCreg(name, len)`.\n\nExample: create classical 5-bit register named `ans`:\n```javascript\ncircuit.createCreg(\"ans\", 5);\n```\n\n**Read register**\n\nTo get register value as integer, call `getCregValue(name)`.\n\nExample:\n```javascript\nvar value = circuit.getCregValue(\"ans\");\n```\n\n**Read all registers as dictionary**\n\n```javascript\nvar regs = circuit.getCregs();\nconsole.log(regs);\n```\n\n**Read all registers as tab delimited CSV string**\n\n```javascript\nvar tsv = circuit.cregsAsString();\nconsole.log(tsv);\n```\n\n**Read single bit**\n\nExample: get bit 3 from register named `ans`:\n\n```javascript\nconsole.log(circuit.getCregBit(\"ans\", 3));\n```\n*Returns integer: 0 or 1*\n\n\n**Set single bit**\n\nExample: set bit 3 to `1` in register named `ans`:\n\n```javascript\ncircuit.setCregBit(\"ans\", 3, 1);\n```\n\n## Control by classical register\n\nEach quatum gate in the circuit (except \"measure\" gate) can be controlled by classical register - gate will be executed only if classical register contains specified value. Pass `options` object as fourth argument to `addGate` method:\n\nExample:\n```javascript\ncircuit.addGate(\"x\", -1, 0, { \n    condition: { \n        creg: \"ans\",\n        value: 7\n    }\n});\n```\nIn this example, \"x\" gate will execute on qubit 0 only if value of register named \"ans\" equals 7.\n\n\n## Reset qubit\n\nYou can reset qubit to value `|0\u003e` or `|1\u003e` with `resetQubit` method:\n\n```javascript\ncircuit.resetQubit(3, 0);\n```\nIn this example, qubit 3 will be set to `0|\u003e`. \n\n*Note that all entangled qubits will be changed as well*\n\n\n## View/print state vector\n\nYou can get state as string with method `stateAsString(onlyPossible)`:\n\n```javascript\nvar s = circuit.stateAsString(false);\n```\n\nIf you want only possible values (only values with probability \u003e 0) then pass `true`:\n```javascript\nvar s = circuit.stateAsString(true);\n```\n\n\nOr, you can print state to javascript console with method `print(onlyPossible)`:\n\n```javascript\ncircuit.print(false);\n```\n\nIf you want to print only possible values (only values with probability \u003e 0) then pass `true`:\n```javascript\nvar s = circuit.print(true);\n```\n\n\n## Save/Load circuit\n\nYou can export circuit to javascript object (format internally used by QuantumCircuit) by calling `save` method:\n\n```javascript\nvar obj = circuit.save();\n\n// now do something with obj, save to file or whatever...\n\n```\n\nAnd load previously saved circuit by calling `load` method:\n\n```javascript\nvar obj = // ...load object from file or from another circuit or whatever\n\ncircuit.load(obj);\n\n```\n\n\n## Use circuit as a gate in another circuit\n\nYou can \"compile\" any circuit and use it as a gate in another circuit like this:\n\n```javascript\n// export circuit to variable\nvar obj = someCircuit.save();\n\n// register it as a gate in another circuit\nanotherCircuit.registerGate(\"my_gate\", obj);\n\n// use it as a gate in another circuit\n// assuming original circuit has three qubits then gate must spread to 3 qubits, in this example: 2, 3, 4)\nanotherCircuit.addGate(\"my_gate\", 0, [2, 3, 4]);\n\n```\n\n\n## Decompose circuit\n\nIf your circuit contains user defined gates (created from another circuit), you can decompose it into equivalent circuit containing only basic gates.\n\nIf you pass `true` as argument to function `save`, you'll get decomposed circuit.\n\nExample:\n```javascript\nvar obj = circuit.save(true);\n// now obj contains decomposed circuit. You can load it:\ncircuit.load(obj);\n```\n\n# Import circuit\n\n## Import from QASM\n\nCircuit can be imported from [OpenQASM](https://github.com/Qiskit/openqasm) with following limitations:\n\n- `import` directive is ignored (but gates defined in `qelib1.inc` are supported) **TODO**\n\n- `barrier` is ignored. **TODO**\n\n\nTo import circuit from OpenQASM use `importQASM(input, errorCallback)` method:\n\nExample:\n```javascript\ncircuit.importQASM(\"OPENQASM 2.0;\\nimport \\\"qelib1.inc\\\";\\nqreg q[2];\\nh q[0];\\ncx q[0],q[1];\\n\", function(errors) {\n    console.log(errors);\n});\n```\n\n- `input` is string containing QASM source code.\n\n- `errorCallback` (optional) callback will be called after parsing with one argument: array containing errors or empty array on success. If no callback is provided, function will throw error if input contains errors.\n\n\n## Import from QUIL\n\nCircuit can be imported from [Quil](https://arxiv.org/abs/1608.03355):\n\n\nTo import circuit from QUIL use `importQuil(quil, errorCallback, options, qubitNames, renamedGates, lineOffset)` method:\n\nExample:\n```javascript\ncircuit.importQuil(\"H 0\\nCNOT 0 1\\n\", function(errors) {\n    console.log(errors);\n});\n```\n\n- `quil` is string containing QUIL source code.\n\n- `errorCallback` (optional) callback will be called after parsing with one argument: array containing errors or empty array on success. If no callback is provided, function will throw error if input contains errors.\n\n- `options` (optional) function will be called after parsing with array containing syntax errors.\n\n- `qubitNames` (optional) names to be given to the qubits.\n\n- `renamedGates` (optional) custom names given to basic commands\n\n- `lineOffset` (optional) no. of spaces before a new line\n\n\n## Import from Qobj\n\nCircuit can be imported from [Qobj](https://qiskit.org/documentation/apidoc/qobj.html):\n\nTo import circuit from OpenQASM use `importQobj(qobj, errorCallback)` method:\n\nExample:\n```javascript\ncircuit.importQobj({\"qobj_id\":\"qobj_WlLkcGHxihyqWGrKEZ\",\"type\":\"QASM\",\"schema_version\":\"1.0\",\"experiments\":[{\"header\":{\"memory_slots\":0,\"n_qubits\":2,\"qreg_sizes\":[[\"q\",2]],\"qubit_labels\":[[\"q\",0],[\"q\",1]],\"creg_sizes\":[],\"clbit_labels\":[],\"name\":\"circuit0\",\"description\":\"text_exp\"},\"config\":{\"n_qubits\":2,\"memory_slots\":0},\"instructions\":[{\"name\":\"x\",\"qubits\":[0,1]}]}],\"header\":{\"description\":\"test_circ\"},\"config\":{\"shots\":1,\"memory_slots\":0}}, function(errors) {\n    console.log(errors);\n});\n```\n\n- `qobj` is Qobj JSON (`\"type\": \"QASM\"`).\n\n- `errorCallback` (optional) callback will be called after parsing with one argument: array containing errors or empty array on success. If no callback is provided, function will throw error if input contains errors.\n\n\n## Import from IONQ json\n\nCircuit can be imported from [IONQ json](https://docs.ionq.com/#tag/quantum_programs):\n\n\nTo import circuit from IONQ json use `importIonq(data, errorCallback)` method:\n\nExample:\n```javascript\nvar ionqCircuit = {\n  \"qubits\": 2,\n  \"circuit\": [\n    {\n      \"gate\": \"h\",\n      \"target\": 0\n    },\n    {\n      \"gate\": \"cnot\",\n      \"target\": 1,\n      \"control\": 0\n    }\n  ]\n};\n\ncircuit.importIonq(ionqCircuit, function(errors) {\n    console.log(errors);\n});\n```\n\n- `data` is IONQ JSON object.\n\n- `errorCallback` (optional) callback will be called after parsing with one argument: array containing errors or empty array on success. If no callback is provided, function will throw error if input contains errors.\n\n\n# Export circuit\n\n## Export to JavaScript\n\nCircuit can be exported to JavaScript with `exportJavaScript(comment, decompose, null, asJupyter)` method:\n\nExample:\n```javascript\nvar js = circuit.exportJavaScript(\"Comment to insert at the beginning.\\nCan be multi-line comment like this one.\", false);\n```\n\n- `comment` - comment to insert at the beginning of the file.\n\n- `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n- `asJupyter` - when this argument is `true` jupyter notebook (set to use `ijavascript` kernel) will be returned.\n\n\n## Export to Qiskit (python)\n\nCircuit can be exported to [Qiskit](https://qiskit.org/documentation/) with following limitation:\n\n- User defined gates are not generated. Instead, circuit is decomposed to basic gates and exported. Effect is the same but code is less readable. **TODO**\n\n- Gates not directly supported by Qiskit are exported as-is - their definition is not generated. **TODO**\n\nTo export circuit to Qiskit use `exportToQiskit(options, exportAsGateName, circuitReplacement, insideSubmodule)` method :\n\nExample:\n```javascript\nvar qiskit = circuit.exportToQiskit({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false, null, null);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `versionStr` - Qiskit version. Can be `\"0.7\"`. Exports to latest supported version when empty string is provided. Remember - it is a string.\n\n    - `providerName` - name of the Qiskit backend simulator provider.\n\n    - `backendName` - name of the Qiskit backend simulator.\n\n    - `asJupyter` - when this argument is `true` jupyter notebook will be returned.\n\n    - `shots` - no. of trials.\n\n    - `hybrid` - when `true` exports user defined cost function along with circuit for hybrid Quantum-Classical Algorithms\n\n- `insideSubmodule` - used internally (when `true` adds extra indent for alignment)\n\n- `exportAsGateName` - used internally (name of the custom gate containing the Qiskit circuit)\n\n- `circuitReplacement` - used internally (when `true` exports only gates in the circuit)\n\n\n## Export to QASM\n\nCircuit can be exported to [OpenQASM](https://github.com/Qiskit/openqasm) with following limitation:\n\n- at the moment, gates not directly supported by QASM and qelib1.inc are exported as-is - their definition is not generated. **TODO**\n\nTo export circuit to OpenQASM use `exportToQASM(options, exportAsGateName, circuitReplacement, insideSubmodule)` method:\n\nExample:\n```javascript\nvar qasm = circuit.exportToQASM({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `compatibilityMode` - if set to `true` exports the circuit in compatible mode\n\n- `insideSubmodule` - when `true` adds extra indent for alignment\n\n- `exportAsGateName` - name of the custom gate containing the Qiskit circuit.\n\n- `circuitReplacement` - when `true` exports only gates in the circuit\n\n\n\n## Export to pyQuil (python)\n\nCircuit can be exported to [pyQuil](http://docs.rigetti.com/en/latest/index.html)\n\nTo export circuit to pyQuil use `exportToPyquil(options, exportAsGateName)` method:\n\nExample:\n```javascript\nvar qasm = circuit.exportToPyquil({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `versionStr` - pyQuil version. Can be `\"1.9\"`, `\"2.0\"` or `\"2.1\"`. Exports to latest supported version when empty string is provided. Remember - it is a string.\n\n    - `lattice` - You can optionally pass then name of the lattice.\n\n    - `asQVM` - If this argument is `true` (and if `lattice` is specified) then produced code will run on QVM mimicking running on QPU. Otherwise, produced code will run on QPU.\n\n    - `asJupyter` - when this argument is `true` jupyter notebook will be returned.\n\n    - `shots` - no. of trials.\n\n    - `hybrid` - when `true` exports user defined cost function along with circuit for hybrid Quantum-Classical Algorithms\n\n- `exportAsGateName` - name of the custom gate containing the Pyquil circuit.\n\n\n## Export to Quil\n\nCircuit can be exported to [Quil](https://arxiv.org/abs/1608.03355)\n\nTo export circuit to Quil use `exportToQuil(options, exportAsGateName)` method:\n\nExample:\n```javascript\nvar quil = circuit.exportToQuil({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `versionStr` - pyQuil version. Can be `\"1.9\"`, `\"2.0\"` or `\"2.1\"`. Exports to latest supported version when empty string is provided. Remember - it is a string.\n\n- `exportAsGateName` - name of the custom gate containing the Pyquil circuit.\n\n\n\n## Export to Cirq (python)\n\nCircuit can be exported to [Cirq](https://github.com/quantumlib/Cirq) with following limitation:\n\n- Gates not directly supported by Cirq are exported as-is - their definition is not generated. **TODO**\n\n- Classical control is ignored (comment with warning is generated). **TODO**\n\nTo export circuit to Cirq use `exportToCirq(options, exportAsGateName)` method:\n\nExample:\n```javascript\nvar cirq = circuit.exportToCirq({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `versionStr` - Cirq version. Can be `\"0.5\"` or empty string. Exports to latest supported version when empty string is provided. Remember - it is a string.\n\n    - `asJupyter` - when this argument is `true` jupyter notebook will be returned.\n\n    - `shots` - no. of trials.\n\n    - `exportTfq` - if set to `true` the export function will export circuit to Tensorflow Quantum.\n\n- `exportAsGateName` - name of the custom gate containing the Cirq circuit.\n\n\n## Export to C/C++ (QuEST)\n\nCircuit can be exported to [QuEST](https://quest.qtechtheory.org/)\n\nTo export circuit to QuEST use `exportQuEST(newOptions, exportAsGateName, definedFunc)` method:\n\nExample:\n```javascript\nvar quest = circuit.exportToQuEST(\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\", false);\n```\n\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n- `exportAsGateName` - name of the custom gate containing the Cirq circuit.\n\n- `definedFunc` - list of gates that must be present in the defined function\n\n\n\n## Export to Q# (QSharp)\n\nCircuit can be exported to [Q#](https://docs.microsoft.com/en-us/quantum/language/index?view=qsharp-preview).\n\nTo export circuit to Q# use `exportQSharp(options, exportAsGateName)` method:\n\nExample:\n```javascript\nvar qsharp = circuit.exportQSharp(\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\", false, null, null, false, null);\n```\n\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `versionStr` - QSharp version. Can be `\"0.1\"` or empty string. Exports to latest supported version when empty string is provided. Remember - it is a string.\n\n    - `asJupyter` - when this argument is `true` jupyter notebook (set to use qsharp kernel) will be returned.\n\n    - `circuitName` - Name of the circuit that is being exported to QSharp. By default set to `\"Circuit\"`\n\n    - `indentDepth` - The no. of tabs to be put before a Python line of code.\n\n- `exportAsGateName` - name of the custom gate containing the QSharp circuit.\n\n\n## Export to Qobj\n\nCircuit can be exported to [Qobj](https://qiskit.org/documentation/apidoc/qobj.html):\n\nTo export circuit to Qiskit use `exportToQobj(options, circuitReplacement)` method :\n\nExample:\n```javascript\nvar qobj = circuit.exportToQobj({circuitName:\"new_circuit\"}, false);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `circuitName` - name of the circuit that is being exported to Qobj\n\n    - `experimentName` - name of the experiment that describes the number of memory slots, qubits, qubit names, classical bit names etc. \n\n    - `numShots` - no. of trials.\n\n- `circuitReplacement` - when `true` exports only gates in the circuit\n\n\n## Export to TensorFlow Quantum (python)\n\nCircuit can be exported to [Tensorflow Quantum](https://www.tensorflow.org/quantum):\n\nTo export circuit to TFQ use `exportToTFQ(options, exportAsGateName)` method :\n\nExample:\n```javascript\nvar tfq = circuit.exportToTFQ({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `versionStr` - TFQ version. Exports to latest supported version when empty string is provided. Remember - it is a string.\n\n    - `asJupyter` - when this argument is `true` jupyter notebook will be returned.\n\n    - `shots` - no. of trials.\n\n- `exportAsGateName` - name of the custom gate containing the TFQ circuit.\n\n\n## Export to Braket (python)\n\nCircuit can be exported to [Braket](https://docs.aws.amazon.com/braket/):\n\nTo export circuit to Braket use `exportToBraket(options, exportAsGateName)` method :\n\nExample:\n```javascript\nvar braket = circuit.exportToBraket({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `versionStr` - Braket version. Exports to latest supported version when empty string is provided. Remember - it is a string.\n\n    - `asJupyter` - when this argument is `true` jupyter notebook will be returned.\n\n    - `shots` - no. of trials.\n\n    - `indentDepth` - The no. of tabs to be put before a Python line of code.\n\n    - `hybrid` - when `true` exports user defined cost function along with circuit for hybrid Quantum-Classical Algorithms\n\n- `exportAsGateName` - name of the custom gate containing the Braket circuit.\n\n\n## Export to pyAQASM (python)\n\nCircuit can be exported to [pyAQASM](https://myqlm.github.io/):\n\nTo export circuit to pyAQASM use `exportToPyAQASM(options, exportAsGateName)` method :\n\nExample:\n```javascript\nvar pyAqasm = circuit.exportToPyAQASM({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `asJupyter` - when this argument is `true` jupyter notebook will be returned.\n\n    - `shots` - no. of trials.\n\n    - `hybrid` - when `true` exports user defined cost function along with circuit for hybrid Quantum-Classical Algorithms\n\n- `exportAsGateName` - name of the custom gate containing the pyAQASM circuit.\n\n\n## Export to AQASM\n\nCircuit can be exported to [AQASM](https://myqlm.github.io/aqasm.html):\n\nTo export circuit to AQASM use `exportToAQASM(options, isExportPyAQASM, exportAsGateName, indentDepth)` method :\n\nExample:\n```javascript\nvar aqasm = circuit.exportToAQASM({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false);\n```\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `asJupyter` - when this argument is `true` jupyter notebook will be returned.\n\n    - `shots` - no. of trials.\n\n    - `hybrid` - when `true` exports user defined cost function along with circuit for hybrid Quantum-Classical Algorithms\n\n- `isExportPyAQASM` - if `true`, this function will be used to export to pyAQASM instead of AQASM.\n\n- `exportAsGateName` - name of the custom gate containing the AQASM circuit.\n\n- `indentDepth` - The no. of tabs to be put before a Python line of code.\n\n\n\n## Export to CudaQ (python)\n\nCircuit can be exported to [CudaQ](https://nvidia.github.io/cuda-quantum/latest/index.html)\n\nTo export circuit to CudaQ use `exportToCudaQ(options, exportAsGateName, circuitReplacement, insideSubmodule)` method :\n\nExample:\n```javascript\nvar cudaq = circuit.exportToCudaQ({comment:\"Comment to insert at the beginning.\\nCan be multi-line comment as this one.\"}, false, null, null);\n```\n\n- `options` - consists of parameters for circuit export as follows:\n\n    - `comment` - comment to insert at the beginning of the file.\n\n    - `decompose` - if set to `true` and circuit contains user defined gates then it will be decomposed to basic gates and then exported. If set to `false` then user defined gates will exported as subroutines.\n\n    - `backendName` - name of the CudaQ backend.\n\n    - `asJupyter` - when this argument is `true` jupyter notebook will be returned.\n\n    - `shots` - number of shots (if code is sampling).\n\n    - `hybrid` - when `true` exports user defined cost function along with circuit for hybrid Quantum-Classical Algorithms\n\n- `insideSubmodule` - used internally (when `true` adds extra indent for alignment)\n\n- `exportAsGateName` - used internally (name of the custom gate containing the CudaQ circuit)\n\n- `circuitReplacement` - used internally (when `true` exports only gates in the circuit)\n\n\n\n\n## Export to SVG\n\nVector `.svg` image of circuit can be created with `exportSVG(embedded)` function with following limitations:\n\n- Gate symbols are non-standard. **TODO** *(BTW, do we have standard?)*\n\n\n**Example 1**\n\nShow circuit in browser:\n\n```javascript\n\n// Assuming we have \u003cdiv id=\"drawing\"\u003e\u003c/div\u003e somewhere in HTML\nvar container = document.getElementById(\"drawing\");\n\n// SVG is returned as string\nvar svg = circuit.exportSVG(true);\n\n// add SVG into container\ncontainer.innerHTML = svg;\n\n```\n\n**Example 2**\n\nGenerate standalone SVG image at server with node.js:\n\n```javascript\n\n// export as standalone SVG\nvar svg = circuit.exportSVG(false);\n\n// do something with svg string (e.g. save to file)\n...\n\n// Or, export as embedded SVG for use in browser\nsvg = circuit.exportSVG(true);\n\n// do something with svg string (e.g. serve via HTTP)\n...\n\n``` \n\n\n## Export to Quirk\n\nCircuit can be exported to popular open-source drag-and-drop quantum circuit simulator [Quirk](https://algassert.com/quirk) with following limitations:\n\n- Quirk doesn't support more than 16 qubits.\n\n- Quirk can possibly incorrectly interpret circuit if we have multiple controlled gates in the same column.\n\n- Quirk doesn't support non-sequentially positioned multi-qubit user-defined gates (for example gate on wires [3, 0, 1]) so it's best to export decomposed circuit.\n\n\nExample:\n\n```javascript\n\nvar quirkData = circuit.exportQuirk(true);\n\nvar quirkURL = \"http://algassert.com/quirk#circuit=\" + JSON.stringify(quirkData);\n\n// Now do something with quirkURL. Assuming this code runs in browser and we have \u003ca id=\"quirk\"\u003e\u003c/a\u003e somewhere, you can:\nvar quirkLink = document.getElementById(\"quirk\");\nquirkLink.setAttr(\"href\", quirkLink);\n\n```\n\n\n# About simulator algorithm\n\nMemory usage: up to `2 * (2^numQubits) * sizeOfComplexNumber`\n\n\n- Naive implementation stores entire state vector in an array of size `2^numQubits`. We are storing state in a \"map\", and only amplitudes with non-zero probabilities are stored. So, in worst case, size of state map is `2^n`, but it's less most of the time because we don't store zeroes.\n\n- Naive implementation creates transformation matrix and multiplies it with state vector. We are not creating and not storing entire transformation matrix in memory. Instead, elements of transformation matrix are calculated one by one and state is multiplied and stored in new state map on the fly. This way, memory usage is minimal (in worst case we have two `2^n` state vectors at a time).\n\n- Algorithm is parallelizable so it could use GPU, but GPU support is not implemented yet (work in progress).\n\n\n## Benchmark\n\n*Performance is measured on MacBook Pro MJLT2 mid-2015 (Core i7 2.5 GHz, 16GB RAM)*\n\n![Benchmark 1](https://rawgit.com/perak/quantum-circuit/HEAD/media/benchmark1.png)\n\n![Benchmark 2](https://rawgit.com/perak/quantum-circuit/HEAD/media/benchmark2.png)\n\n![Benchmark 3](https://rawgit.com/perak/quantum-circuit/HEAD/media/benchmark3.png)\n\n*You can find scripts in [/benchmark](benchmark/) directory.*\n\n\n# Gates\n\n## id\n\nSingle qubit identity gate\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,1]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"id\", 0);\n```\n\n## x\n\nPauli X (PI rotation over X-axis) aka \"NOT\" gate\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [0,1],\n    [1,0]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"x\", 0);\n```\n\n## y\n\nPauli Y (PI rotation over Y-axis)\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [0,\"-i\"],\n    [\"i\",0]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"y\", 0);\n```\n\n## z\n\nPauli Z (PI rotation over Z-axis)\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,-1]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"z\", 0);\n```\n\n## h\n\nHadamard gate\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [\"1 / sqrt(2)\",\"1 / sqrt(2)\"],\n    [\"1 / sqrt(2)\",\"-1 / sqrt(2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"h\", 0);\n```\n\n## srn\n\nSquare root of NOT\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [\"0.5+0.5i\",\"0.5-0.5i\"],\n    [\"0.5-0.5i\",\"0.5+0.5i\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"srn\", 0);\n```\n\n## srndg\n\nInverse square root of NOT\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [\"0.5-0.5i\",\"0.5+0.5i\"],\n    [\"0.5+0.5i\",\"0.5-0.5i\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"srndg\", 0);\n```\n\n## r2\n\nPI/2 rotation over Z-axis aka \"Phase PI/2\"\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,\"exp(i * pi / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"r2\", 0);\n```\n\n## r4\n\nPI/4 rotation over Z-axis aka \"Phase PI/4\"\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,\"exp(i * pi / 4)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"r4\", 0);\n```\n\n## r8\n\nPI/8 rotation over Z-axis aka \"Phase PI/8\"\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,\"exp(i * pi / 8)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"r8\", 0);\n```\n\n## rx\n\nRotation around the X-axis by given angle\n\n**Qubits:** 1\n\n**Parameters:**\n\n- theta\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"cos(theta / 2)\",\"-i * sin(theta / 2)\"],\n    [\"-i * sin(theta / 2)\",\"cos(theta / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"rx\", 0, {\n    params: {\n        theta: \"pi/2\"\n    }\n});\n```\n\n## ry\n\nRotation around the Y-axis by given angle\n\n**Qubits:** 1\n\n**Parameters:**\n\n- theta\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"cos(theta / 2)\",\"-1 * sin(theta / 2)\"],\n    [\"sin(theta / 2)\",\"cos(theta / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"ry\", 0, {\n    params: {\n        theta: \"pi/2\"\n    }\n});\n```\n\n## rz\n\nRotation around the Z-axis by given angle\n\n**Qubits:** 1\n\n**Parameters:**\n\n- phi\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"cos(phi / 2) - i * sin(phi / 2)\",0],\n    [0,\"cos(phi / 2) + i * sin(phi / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"rz\", 0, {\n    params: {\n        phi: \"pi/2\"\n    }\n});\n```\n\n## u1\n\nSingle-qubit rotation about the Z axis\n\n**Qubits:** 1\n\n**Parameters:**\n\n- lambda\n\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,\"exp(i * lambda)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"u1\", 0, {\n    params: {\n        lambda: \"pi/2\"\n    }\n});\n```\n\n## u2\n\nSingle-qubit rotation about the X+Z axis\n\n**Qubits:** 1\n\n**Parameters:**\n\n- phi\n- lambda\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"1 / sqrt(2)\",\"-exp(i * lambda) * 1 / sqrt(2)\"],\n    [\"exp(i * phi) * 1 / sqrt(2)\",\"exp(i * lambda + i * phi) * 1 / sqrt(2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"u2\", 0, {\n    params: {\n        phi: \"pi/2\",\n        lambda: \"pi/2\"\n    }\n});\n```\n\n## u3\n\nGeneric single-qubit rotation gate with 3 Euler angles\n\n**Qubits:** 1\n\n**Parameters:**\n\n- theta\n- phi\n- lambda\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"cos(theta/2)\",\"-exp(i * lambda) * sin(theta / 2)\"],\n    [\"exp(i * phi) * sin(theta / 2)\",\"exp(i * lambda + i * phi) * cos(theta / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"u3\", 0, {\n    params: {\n        theta: \"pi/2\",\n        phi: \"pi/2\",\n        lambda: \"pi/2\"\n    }\n});\n```\n\n## s\n\nPI/2 rotation over Z-axis (synonym for `r2`)\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,\"i\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"s\", 0);\n```\n\n## t\n\nPI/4 rotation over Z-axis (synonym for `r4`)\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,\"exp(i * pi / 4)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"t\", 0);\n```\n\n## sdg\n\n(-PI/2) rotation over Z-axis\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,\"-i\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"sdg\", 0);\n```\n\n## tdg\n\n(-PI/4) rotation over Z-axis\n\n**Qubits:** 1\n\n**Matrix:**\n```javascript\n[\n\n    [1,0],\n    [0,\"exp(-i * pi / 4)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"tdg\", 0);\n```\n\n## gpi\n\nGPi gate\n\n**Qubits:** 1\n\n**Parameters:**\n\n- phi\n\n\n**Matrix:**\n```javascript\n[\n\n    [0,\"exp(-i*phi)\"],\n    [\"exp(i*phi)\",0]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"gpi\", 0, {\n    params: {\n        phi: \"pi/2\"\n    }\n});\n```\n\n## gpi2\n\nGPi2 gate\n\n**Qubits:** 1\n\n**Parameters:**\n\n- phi\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"1/sqrt(2)\",\"(-i*exp(-i*phi))/sqrt(2)\"],\n    [\"(-i*exp(i*phi))/sqrt(2)\",\"1/sqrt(2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"gpi2\", 0, {\n    params: {\n        phi: \"pi/2\"\n    }\n});\n```\n\n## vz\n\nVirtualZ gate\n\n**Qubits:** 1\n\n**Parameters:**\n\n- theta\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"exp(-i*theta/2)\",0],\n    [0,\"exp(i*theta/2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"vz\", 0, {\n    params: {\n        theta: \"pi/2\"\n    }\n});\n```\n\n## cx\n\nControlled NOT (CNOT) gate\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,0,1],\n    [0,0,1,0]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cx\", [0, 1]);\n```\n\n## cy\n\nControlled Y gate (controlled rotation over Y-axis by PI)\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,0,\"-i\"],\n    [0,0,\"i\",0]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cy\", [0, 1]);\n```\n\n## cz\n\nControlled Z gate (controlled rotation over Z-axis by PI)\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,1,0],\n    [0,0,0,-1]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cz\", [0, 1]);\n```\n\n## ch\n\nControlled Hadamard gate\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,\"1 / sqrt(2)\",\"1 / sqrt(2)\"],\n    [0,0,\"1 / sqrt(2)\",\"-1 / sqrt(2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"ch\", [0, 1]);\n```\n\n## csrn\n\nControlled square root of NOT\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,\"0.5+0.5i\",\"0.5-0.5i\"],\n    [0,0,\"0.5-0.5i\",\"0.5+0.5i\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"csrn\", [0, 1]);\n```\n\n## swap\n\nSwaps the state of two qubits.\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,0,1,0],\n    [0,1,0,0],\n    [0,0,0,1]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"swap\", [0, 1]);\n```\n\n## srswap\n\nSquare root of swap\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,\"0.5 * (1 + i)\",\"0.5 * (1 - i)\",0],\n    [0,\"0.5 * (1 - i)\",\"0.5 * (1 + i)\",0],\n    [0,0,0,1]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"srswap\", [0, 1]);\n```\n\n## iswap\n\nSwaps the state of two qubits, applying a -i phase to q1 when it is in the 1 state and a -i phase to q2 when it is in the 0 state\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,0,\"0+i\",0],\n    [0,\"0+i\",0,0],\n    [0,0,0,1]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"iswap\", [0, 1]);\n```\n\n## xx\n\nXX gate\n\n**Qubits:** 2\n\n**Parameters:**\n\n- theta\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"cos(theta)\",0,0,\"-i*sin(theta)\"],\n    [0,\"cos(theta)\",\"-i*sin(theta)\",0],\n    [0,\"-i*sin(theta)\",\"cos(theta)\",0],\n    [\"-i*sin(theta)\",0,0,\"cos(theta)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"xx\", [0, 1], {\n    params: {\n        theta: \"pi/2\"\n    }\n});\n```\n\n## yy\n\nYY gate\n\n**Qubits:** 2\n\n**Parameters:**\n\n- theta\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"cos(theta)\",0,0,\"i*sin(theta)\"],\n    [0,\"cos(theta)\",\"-i*sin(theta)\",0],\n    [0,\"-i*sin(theta)\",\"cos(theta)\",0],\n    [\"i*sin(theta)\",0,0,\"cos(theta)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"yy\", [0, 1], {\n    params: {\n        theta: \"pi/2\"\n    }\n});\n```\n\n## zz\n\nParametric 2-qubit rotation about ZZ\n\n**Qubits:** 2\n\n**Parameters:**\n\n- theta\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"exp(-i * theta / 2)\",0,0,0],\n    [0,\"exp(i * theta / 2)\",0,0],\n    [0,0,\"exp(i * theta / 2)\",0],\n    [0,0,0,\"exp(-i * theta / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"zz\", [0, 1], {\n    params: {\n        theta: \"pi/2\"\n    }\n});\n```\n\n## xy\n\nXY gate\n\n**Qubits:** 2\n\n**Parameters:**\n\n- phi\n\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,\"cos(phi / 2)\",\"i * sin(phi / 2)\",0],\n    [0,\"i * sin(phi / 2)\",\"cos(phi / 2)\",0],\n    [0,0,0,1]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"xy\", [0, 1], {\n    params: {\n        phi: \"pi/2\"\n    }\n});\n```\n\n## ms\n\nMølmer-Sørensen gate\n\n**Qubits:** 2\n\n**Parameters:**\n\n- phi0\n- phi1\n\n\n**Matrix:**\n```javascript\n[\n\n    [\"1/sqrt(2)\",0,0,\"(-i*exp(-i*(phi0+phi1)))/sqrt(2)\"],\n    [0,\"1/sqrt(2)\",\"(-i*exp(-i*(phi0-phi1)))/sqrt(2)\",0],\n    [0,\"(-i*exp(i*(phi0-phi1)))/sqrt(2)\",\"1/sqrt(2)\",0],\n    [\"(-i*exp(i*(phi0+phi1)))/sqrt(2)\",0,0,\"1/sqrt(2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"ms\", [0, 1], {\n    params: {\n        phi0: \"pi/2\",\n        phi1: \"pi/2\"\n    }\n});\n```\n\n## cr2\n\nControlled PI/2 rotation over Z-axis\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,1,0],\n    [0,0,0,\"exp(i * pi / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cr2\", [0, 1]);\n```\n\n## cr4\n\nControlled PI/4 rotation over Z-axis\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,1,0],\n    [0,0,0,\"exp(i * pi / 4)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cr4\", [0, 1]);\n```\n\n## cr8\n\nControlled PI/8 rotation over Z-axis\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,1,0],\n    [0,0,0,\"exp(i * pi / 8)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cr8\", [0, 1]);\n```\n\n## crx\n\nControlled rotation around the X-axis by given angle\n\n**Qubits:** 2\n\n**Parameters:**\n\n- theta\n\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,\"cos(theta / 2)\",\"-i * sin(theta / 2)\"],\n    [0,0,\"-i * sin(theta / 2)\",\"cos(theta / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"crx\", [0, 1], {\n    params: {\n        theta: \"pi/2\"\n    }\n});\n```\n\n## cry\n\nControlled rotation around the Y-axis by given angle\n\n**Qubits:** 2\n\n**Parameters:**\n\n- theta\n\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,\"cos(theta / 2)\",\"-1 * sin(theta / 2)\"],\n    [0,0,\"sin(theta / 2)\",\"cos(theta / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cry\", [0, 1], {\n    params: {\n        theta: \"pi/2\"\n    }\n});\n```\n\n## crz\n\nControlled rotation around the Z-axis by given angle\n\n**Qubits:** 2\n\n**Parameters:**\n\n- phi\n\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,\"cos(phi / 2) - i * sin(phi / 2)\",0],\n    [0,0,0,\"cos(phi / 2) + i * sin(phi / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"crz\", [0, 1], {\n    params: {\n        phi: \"pi/2\"\n    }\n});\n```\n\n## cu1\n\nControlled rotation about the Z axis\n\n**Qubits:** 2\n\n**Parameters:**\n\n- lambda\n\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,1,0],\n    [0,0,0,\"exp(i * lambda)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cu1\", [0, 1], {\n    params: {\n        lambda: \"pi/2\"\n    }\n});\n```\n\n## cu2\n\nControlled rotation about the X+Z axis\n\n**Qubits:** 2\n\n**Parameters:**\n\n- phi\n- lambda\n\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,\"1 / sqrt(2)\",\"-exp(i * lambda) * 1 / sqrt(2)\"],\n    [0,0,\"exp(i * phi) * 1 / sqrt(2)\",\"exp(i * lambda + i * phi) * 1 / sqrt(2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cu2\", [0, 1], {\n    params: {\n        phi: \"pi/2\",\n        lambda: \"pi/2\"\n    }\n});\n```\n\n## cu3\n\nControlled rotation gate with 3 Euler angles\n\n**Qubits:** 2\n\n**Parameters:**\n\n- theta\n- phi\n- lambda\n\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,\"cos(theta/2)\",\"-exp(i * lambda) * sin(theta / 2)\"],\n    [0,0,\"exp(i * phi) * sin(theta / 2)\",\"exp(i * lambda + i * phi) * cos(theta / 2)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cu3\", [0, 1], {\n    params: {\n        theta: \"pi/2\",\n        phi: \"pi/2\",\n        lambda: \"pi/2\"\n    }\n});\n```\n\n## cs\n\nControlled PI/2 rotation over Z-axis (synonym for `cr2`)\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,1,0],\n    [0,0,0,\"i\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cs\", [0, 1]);\n```\n\n## ct\n\nControlled PI/4 rotation over Z-axis (synonym for `cr4`)\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,1,0],\n    [0,0,0,\"exp(i * pi / 4)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"ct\", [0, 1]);\n```\n\n## csdg\n\nControlled (-PI/2) rotation over Z-axis\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,1,0],\n    [0,0,0,\"-i\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"csdg\", [0, 1]);\n```\n\n## ctdg\n\nControlled (-PI/4) rotation over Z-axis\n\n**Qubits:** 2\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0],\n    [0,1,0,0],\n    [0,0,1,0],\n    [0,0,0,\"exp(-i * pi / 4)\"]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"ctdg\", [0, 1]);\n```\n\n## ccx\n\nToffoli aka \"CCNOT\" gate\n\n**Qubits:** 3\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0,0,0,0,0],\n    [0,1,0,0,0,0,0,0],\n    [0,0,1,0,0,0,0,0],\n    [0,0,0,1,0,0,0,0],\n    [0,0,0,0,1,0,0,0],\n    [0,0,0,0,0,1,0,0],\n    [0,0,0,0,0,0,0,1],\n    [0,0,0,0,0,0,1,0]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"ccx\", [0, 1, 2]);\n```\n\n## cswap\n\nControlled swap aka \"Fredkin\" gate\n\n**Qubits:** 3\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0,0,0,0,0],\n    [0,1,0,0,0,0,0,0],\n    [0,0,1,0,0,0,0,0],\n    [0,0,0,1,0,0,0,0],\n    [0,0,0,0,1,0,0,0],\n    [0,0,0,0,0,0,1,0],\n    [0,0,0,0,0,1,0,0],\n    [0,0,0,0,0,0,0,1]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"cswap\", [0, 1, 2]);\n```\n\n## csrswap\n\nControlled square root of swap\n\n**Qubits:** 3\n\n**Matrix:**\n```javascript\n[\n\n    [1,0,0,0,0,0,0,0],\n    [0,1,0,0,0,0,0,0],\n    [0,0,1,0,0,0,0,0],\n    [0,0,0,1,0,0,0,0],\n    [0,0,0,0,1,0,0,0],\n    [0,0,0,0,0,\"0.5 * (1 + i)\",\"0.5 * (1 - i)\",0],\n    [0,0,0,0,0,\"0.5 * (1 - i)\",\"0.5 * (1 + i)\",0],\n    [0,0,0,0,0,0,0,1]\n]\n```\n\n**Example:**\n```javascript\ncircuit.appendGate(\"csrswap\", [0, 1, 2]);\n```\n\n## reset\n\nResets qubit\n\n**Qubits:** 1\n\n**Example:**\n```javascript\ncircuit.appendGate(\"reset\", 0);\n```\n\n## measure\n\nMeasures qubit and stores outcome (0 or 1) into classical register\n\n**Qubits:** 1\n\n**Example:**\n```javascript\ncircuit.appendGate(\"measure\", 0, {\n    creg: {\n        name: \"c\",\n        bit: 3\n    }\n});\n```\n\n**Or:**\n```javascript\ncircuit.addMeasure(0, \"c\", 3);\n```\n\n## barrier\n\nBarrier\n\n**Qubits:** 1\n\n**Example:**\n```javascript\ncircuit.appendGate(\"barrier\", 0);\n```\n\n\n\n\n# API docs\n\n*To be written...*\n\n\n# License\n[MIT](LICENSE.txt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantastica%2Fquantum-circuit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquantastica%2Fquantum-circuit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantastica%2Fquantum-circuit/lists"}