### Example: Get Counts from Discopy Circuit with Mock Backend Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Circuit This example demonstrates how to use the `get_counts` method with a `discopy.quantum` circuit and a `mockBackend` from `pytket`. It shows initializing a circuit, setting up a mock backend with predefined counts, and then retrieving normalized counts from the circuit. ```python >>> from discopy.quantum import * >>> circuit = H @ X >> CX >> Measure(2) >>> from discopy.quantum.tk import mockBackend >>> backend = mockBackend({(0, 1): 512, (1, 0): 512}) >>> circuit.get_counts(backend=backend, n_shots=2**10) {(0, 1): 0.5, (1, 0): 0.5} ``` -------------------------------- ### Install DisCoPy Library Source: https://docs.discopy.org/en/main/main This snippet provides the command to install the DisCoPy library using pip, the Python package installer. It is the first step to setting up DisCoPy in your environment, allowing you to access its functionalities. ```bash pip install discopy ``` -------------------------------- ### Python Example: Initializing and Evaluating Ket Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.gates.Ket Demonstrates how to initialize a `Ket` object with a bitstring and evaluate it to a `Tensor` representation, verifying its codomain and array values. ```python from discopy.tensor import Dim, Tensor assert Ket(1, 0).cod == qubit ** 2 assert Ket(1, 0).eval()\ == Tensor[complex](dom=Dim(1), cod=Dim(2, 2), array=[0, 0, 1, 0]) ``` -------------------------------- ### Example: Translate PyTket Circuit to DisCoPy Circuit Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Circuit These examples demonstrate how to use the `Circuit.from_tk` static method to convert `pytket.Circuit` instances into DisCoPy circuits. It shows conversions for simple gates, GHZ states, and Bell states, illustrating how DisCoPy handles underlying quantum operations and transformations. ```python from discopy.quantum import * import pytket as tk ``` ```python c = Rz(0.5) @ qubit >> qubit @ Rx(0.25) >> CX assert Circuit.from_tk(c.to_tk()) == c.init_and_discard() ``` ```python tk_GHZ = tk.Circuit(3).H(1).CX(1, 2).CX(1, 0) pprint = lambda c: print(str(c).replace(' >>', '\n >>')) pprint(Circuit.from_tk(tk_GHZ)) circuit = Ket(1, 0) >> CX >> qubit @ Ket(0) @ qubit print(Circuit.from_tk(circuit.to_tk())[3:-3]) ``` ```python bell_state = Circuit.caps(qubit, qubit) bell_effect = bell_state[::-1] circuit = bell_state @ qubit >> qubit @ bell_effect >> Bra(0) pprint(Circuit.from_tk(circuit.to_tk())[3:]) ``` -------------------------------- ### Example Usage of Sim15ansatz for Quantum Circuit Construction Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.ansatze.Sim15ansatz Demonstrates how to instantiate and use the `Sim15ansatz` class to build quantum circuits. The first example shows a 3-qubit circuit with specific parameters, illustrating its foliation. The second example shows a 1-qubit circuit with different parameters. ```python pprint = lambda c: print(str(c.foliation()).replace(' >>', '\n >>')) pprint(Sim15ansatz(3, [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6]])) print(Sim15ansatz(1, [0.1, 0.2, 0.3])) ``` -------------------------------- ### Constructing discopy.quantum.circuit.Ty Instances Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Ty Examples demonstrating the basic construction of `Ty` instances using `Digit` and `Qudit` for classical bits (`bit`) and quantum bits (`qubit`), and illustrating their non-commutative composition. ```python assert bit == Ty(Digit(2)) assert qubit == Ty(Qudit(2)) assert bit @ qubit != qubit @ bit ``` -------------------------------- ### IQPansatz Usage Examples for 1 and 3 Qubits Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.ansatze.IQPansatz Demonstrates how to use `IQPansatz` to construct quantum circuits. The first example shows a 3-qubit IQP ansatz with specific parameters, illustrating its foliation. The second example shows a 1-qubit IQP ansatz, which simplifies to an Euler decomposition. ```python pprint = lambda c: print(str(c.foliation()).replace(' >>', '\n >>')) pprint(IQPansatz(3, [[0.1, 0.2], [0.3, 0.4]])) print(IQPansatz(1, [0.3, 0.8, 0.4])) ``` -------------------------------- ### Convert Discopy Circuit with Ket State to PyTket Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Circuit This example shows the conversion of a Discopy circuit initialized with `Ket` states to a `pytket.Circuit`. It demonstrates how initial `Ket` states are represented as `X` gates in `pytket`. ```python >>> circuit1 = Ket(1, 0) >> CX >> qubit @ Ket(0) @ qubit >>> circuit1.to_tk() tk.Circuit(3).X(0).CX(0, 2) ``` -------------------------------- ### Convert Bell Test Circuit to PyTket Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Circuit This example demonstrates converting a Discopy Bell test circuit into its equivalent `pytket.Circuit` representation using the `to_tk` method. It shows the Discopy circuit construction and the resulting `pytket` circuit. ```python >>> bell_test = H @ qubit >> CX >> Measure() @ Measure() >>> bell_test.to_tk() tk.Circuit(2, 2).H(0).CX(0, 1).Measure(0, 0).Measure(1, 1) ``` -------------------------------- ### DisCoPy API Module Index Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.gates.Rz A comprehensive index of modules, classes, and functions within the DisCoPy library, covering quantum, grammar, and drawing functionalities. This serves as a navigation guide to the API documentation. ```APIDOC discopy.quantum.gates: - Parametrized - Rotation - Rx - Ry - Rz - CU1 - CRz - CRx - Scalar - MixedScalar - Sqrt - sqrt - scalar discopy.quantum.ansatze: - IQPansatz - Sim14ansatz - Sim15ansatz discopy.quantum.zx: - Diagram - Box - Swap - Spider - Z - Y - X - Scalar discopy.quantum.tk: - Circuit - to_tk - from_tk discopy.quantum.pennylane: - PennyLaneCircuit - tk_op_to_pennylane - extract_ops_from_tk - get_post_selection_dict - to_pennylane discopy.grammar.thue: - Word - Rule discopy.grammar.cfg: - Tree - Rule - Word - Id - Operad - Algebra discopy.grammar.categorial: - Diagram - Box - Word - FA - BA - FC - BC - FX - BX - Functor - cat2ty - tree2diagram discopy.grammar.pregroup: - Diagram - Box - Cup - Cap - Swap - Word - Category - Functor - eager_parse - brute_force discopy.grammar.dependency: - from_spacy discopy.drawing.drawing: - Point - PlaneGraph - Drawing - Equation discopy.drawing.backend: - draw - Backend - TikZ - Matplotlib ``` -------------------------------- ### Python Example: Initializing and Using ClassicalGate with SymPy Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.gates.ClassicalGate Demonstrates how to initialize a `ClassicalGate` instance using symbolic data from SymPy and then evaluate its data using the `lambdify` method with concrete values. ```python from sympy import symbols array = symbols("a b c d") f = ClassicalGate('f', bit, bit, array) f.data f.lambdify(*array)(1, 2, 3, 4).data ``` -------------------------------- ### Discopy Library Module and Class Overview Source: https://docs.discopy.org/en/main/main/_api/discopy.compact.Swap A hierarchical overview of the modules, classes, and functions available within the `discopy` library, categorized by their primary domains: quantum, grammar, and drawing. This snippet provides a structured list of API entry points. ```APIDOC discopy.quantum: gates: - Parametrized - Rotation - Rx - Ry - Rz - CU1 - CRz - CRx - Scalar - MixedScalar - Sqrt - sqrt - scalar ansatze: - IQPansatz - Sim14ansatz - Sim15ansatz zx: - Diagram - Box - Swap - Spider - Z - Y - X - Scalar tk: - Circuit - to_tk - from_tk pennylane: - PennyLaneCircuit - tk_op_to_pennylane - extract_ops_from_tk - get_post_selection_dict - to_pennylane discopy.grammar: thue: - Word - Rule cfg: - Tree - Rule - Word - Id - Operad - Algebra categorial: - Diagram - Box - Word - FA - BA - FC - BC - FX - BX - Functor - cat2ty - tree2diagram pregroup: - Diagram - Box - Cup - Cap - Swap - Word - Category - Functor - eager_parse - brute_force dependency: - from_spacy discopy.drawing: drawing: - Point - PlaneGraph - Drawing - Equation backend: - draw - Backend - TikZ - Matplotlib ``` -------------------------------- ### Encode DisCoPy Diagram to Compact Form Source: https://docs.discopy.org/en/main/main/_api/discopy.monoidal.Diagram This example demonstrates how to use the `encode` method to get a compact representation of a diagram and then verify it by decoding it back. ```python x, y, z, w = Ty('x'), Ty('y'), Ty('z'), Ty('w') f0, f1, g = Box('f0', x, y), Box('f1', z, w), Box('g', y @ w, y) diagram = f0 @ f1 >> g dom, boxes_and_offsets = diagram.encode() assert dom == x @ z assert boxes_and_offsets == [(f0, 0), (f1, 1), (g, 0)] assert diagram == Diagram.decode(*diagram.encode()) diagram.draw(path='docs/_static/monoidal/arrow-example.png') ``` -------------------------------- ### Tensor Two Integer Diagrams Source: https://docs.discopy.org/en/main/main/_api/discopy.interaction.Diagram Provides an example setup for tensoring two integer diagrams using `discopy.ribbon.Ty` and `discopy.ribbon.Diagram`. It defines various types necessary for the tensor operation. ```python from discopy.ribbon import Ty as T, Diagram as D, Box as B x, y, u, v = map(Ty[T], "xyuv") x_, y_, u_, v_ = map(lambda x: Ty[T](x + '_'), "xyuv") ``` -------------------------------- ### discopy.quantum.circuit Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.grammar.categorial.Diagram API documentation for the discopy.quantum.circuit module, detailing components for quantum circuits. ```APIDOC discopy.quantum.circuit module: Classes/Functions: - Ob - Digit - Qudit - Ty - Circuit - Box - Sum - Swap - Functor - index2bitstring - bitstring2index ``` -------------------------------- ### Example Usage of discopy.hypergraph.pushout Source: https://docs.discopy.org/en/main/main/_api/discopy.hypergraph.pushout An example demonstrating how to use the `pushout` function with specific inputs and expected outputs. ```python assert pushout(2, 3, [1], [0]) == ({0: 0, 1: 1}, {0: 1, 1: 2, 2: 3}) ``` -------------------------------- ### DisCoPy Quantum Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.feedback.Diagram API documentation for the `discopy.quantum` module, including gates, ansatze, ZX calculus components, and integrations with TKET and PennyLane. ```APIDOC discopy.quantum: gates: - Rotation - Rx - Ry - Rz - CU1 - CRz - CRx - Scalar - MixedScalar - Sqrt - sqrt - scalar ansatze: - IQPansatz - Sim14ansatz - Sim15ansatz zx: - Diagram - Box - Swap - Spider - Z - Y - X - Scalar tk: - Circuit - to_tk - from_tk pennylane: - PennyLaneCircuit - tk_op_to_pennylane - extract_ops_from_tk - get_post_selection_dict - to_pennylane ``` -------------------------------- ### PennyLaneCircuit Class API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.pennylane.PennyLaneCircuit Detailed API documentation for the `discopy.quantum.pennylane.PennyLaneCircuit` class, its constructor, and methods for managing PennyLane quantum circuits, including post-selection, parameter substitution, and device interaction. ```APIDOC class discopy.quantum.pennylane.PennyLaneCircuit(ops, symbols, params, wires, probabilities, post_selection, scale, n_qubits, backend_config, diff_method) Bases: object Description: Implement a pennylane circuit with post-selection. Methods: get_device(backend_config) Description: Return a PennyLane device with the specified backend configuration. initialise_device_and_circuit() Description: Initialise the PennyLane device and circuit when instantiating the PennyLaneCirucit, or loading from disk. contains_sympy() Description: Determine if the circuit parameters are concrete or contain SymPy symbols. Returns: bool - Whether the circuit parameters contain SymPy symbols. initialise_concrete_params(symbol_weight_map) Description: Given concrete values for each of the SymPy symbols, substitute the symbols for the values to obtain concrete parameters, via the param_substitution method. draw() Description: Print a string representation of the circuit similar to qml.draw, but including post-selection. Parameters: symbols (list of sympy.core.symbol.Symbol, default: None) – The symbols from the original DisCoPy circuit. weights (list of torch.FloatTensor, default: None) – The weights to substitute for the symbols. get_valid_states() Description: Determine which of the output states of the circuit are compatible with the post-selections. Returns: list of int - The indices of the circuit output that are compatible with the post-selections. make_circuit() Description: Construct the `qml.Qnode`, a circuit that can be used with autograd to construct hybrid models. Returns: `qml.Qnode` - A Pennylane circuit without post-selection. post_selected_circuit(params) Description: Run the circuit with the given parameters and return the post-selected output. Parameters: params (`torch.FloatTensor`) – The concrete parameters for the gates in the circuit. Returns: `torch.Tensor` - The post-selected output of the circuit. param_substitution(weights) Description: Substitute symbolic parameters (SymPy symbols) with floats. Parameters: weights (list of `torch.FloatTensor`) – The weights to substitute for the symbols. Returns: `torch.FloatTensor` - The concrete (non-symbolic) parameters for the circuit. eval() Description: Evaluate the circuit. The symbols should be those from the original DisCoPy diagram, which will be substituted for the concrete parameters in weights. Parameters: (implicitly symbols and weights from context) ``` -------------------------------- ### Python Example: Initialize discopy.interaction.Int Source: https://docs.discopy.org/en/main/main/_api/discopy.interaction.Int This example demonstrates how to initialize the `Int` construction using `discopy.ribbon.Category` and asserts its equivalence to a category with `Ty` and `Diagram` objects. ```python >>> from discopy.ribbon import Ty as T, Diagram as D, Category >>> assert Int(Category(T, D)) == Category(Ty[T], Diagram[D]) ``` -------------------------------- ### DisCoPy Quantum Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.pivotal.Category API documentation for the `discopy.quantum` module, including gates, ansatze, ZX calculus, TKET, and PennyLane integrations for quantum computing applications. ```APIDOC discopy.quantum: gates: Rotation Rx Ry Rz CU1 CRz CRx Scalar MixedScalar Sqrt sqrt scalar ansatze: IQPansatz Sim14ansatz Sim15ansatz zx: Diagram Box Swap Spider Z Y X Scalar tk: Circuit to_tk from_tk pennylane: PennyLaneCircuit tk_op_to_pennylane extract_ops_from_tk get_post_selection_dict to_pennylane ``` -------------------------------- ### Initialize Types for Hypergraph Examples Source: https://docs.discopy.org/en/main/main/_api/discopy.hypergraph.Hypergraph Initializes `Ty` objects `x`, `y`, and `z` from string labels, which are used as atomic types in subsequent `discopy.frobenius.Hypergraph` examples. ```python x, y, z = map(Ty, "xyz") ``` -------------------------------- ### Python Example: Asserting Digit Instance Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Digit An example demonstrating how to assert the internal structure of a `bit` object, showing it contains a `Digit` instance with dimension 2. ```python assert bit.inside == (Digit(2),) ``` -------------------------------- ### DisCoPy Compact Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.pennylane.tk_op_to_pennylane API documentation for the `discopy.compact` module, listing its core classes and functionalities related to compact categories. ```APIDOC discopy.compact: Box Cup Cap Swap Category Functor ``` -------------------------------- ### Convert Discopy Circuit with SWAP Gates to PyTket Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Circuit This example demonstrates converting a Discopy circuit that includes `SWAP` gates to a `pytket.Circuit`. It illustrates how `SWAP` gates are treated as logical swaps and may simplify in the `pytket` representation. ```python >>> circuit2 = X @ qubit ** 2\ ... >> qubit @ SWAP >> CX @ qubit >> qubit @ SWAP >>> circuit2.to_tk() tk.Circuit(3).X(0).CX(0, 2) ``` -------------------------------- ### Python Example: Instantiating and Comparing discopy.cat.Ob Objects Source: https://docs.discopy.org/en/main/main/_api/discopy.cat.Ob This example demonstrates how to create instances of `discopy.cat.Ob` and verify their equality. Objects with the same name are considered equal, while those with different names are not. ```python x, x_, y = Ob('x'), Ob('x'), Ob('y') assert x == x_ and x != y ``` -------------------------------- ### Python Example: Initialize and Evaluate discopy.quantum.gates.Bra Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.gates.Bra Demonstrates how to initialize a `Bra` object with a bitstring (1, 0) and evaluate its tensor representation. It asserts the domain of the `Bra` object and verifies the resulting tensor array, showing its effect on qubit post-selection. ```python from discopy.tensor import Dim, Tensor assert Bra(1, 0).dom == qubit ** 2 assert Bra(1, 0).eval()\ == Tensor[complex](dom=Dim(2, 2), cod=Dim(1), array=[0, 0, 1, 0]) ``` -------------------------------- ### Discopy Quantum Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.drawing.drawing.Drawing API documentation for the `discopy.quantum` module, including gates, ansatze, ZX calculus, and integrations with TKET and PennyLane. ```APIDOC discopy.quantum: gates: Parametrized Rotation Rx Ry Rz CU1 CRz CRx Scalar MixedScalar Sqrt sqrt scalar ansatze: IQPansatz Sim14ansatz Sim15ansatz zx: Diagram Box Swap Spider Z Y X Scalar tk: Circuit to_tk from_tk pennylane: PennyLaneCircuit tk_op_to_pennylane extract_ops_from_tk get_post_selection_dict to_pennylane ``` -------------------------------- ### Example: Using backend context manager with JAX Source: https://docs.discopy.org/en/main/main/_api/discopy.matrix.backend This Python example demonstrates how to use the `backend` context manager to temporarily switch the matrix computation backend to JAX and verify the change. ```python with backend('jax'): assert type(Matrix([0, 1, 1, 0], 2, 2).array).__module__\ == 'jaxlib.xla_extension' ``` -------------------------------- ### Execute Circuit on pytket Backend Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Circuit Shows how to execute a quantum circuit on a `pytket.Backend` (using a mock backend for demonstration) to obtain real-valued probabilities, demonstrating integration with external quantum computing frameworks. ```python circuit = Ket(0, 0) >> sqrt(2) @ H @ X >> CX >> Measure() @ Bra(0) from discopy.quantum.tk import mockBackend backend = mockBackend({(0, 1): 512, (1, 0): 512}) ``` -------------------------------- ### discopy.drawing.drawing.Drawing.validate_attributes Method API and Example Source: https://docs.discopy.org/en/main/main/_api/discopy.drawing.drawing.Drawing API documentation for the `validate_attributes` method, which checks that the attributes of a drawing are consistent. Includes a Python example demonstrating its usage and the expected `ValueError` when attributes are inconsistent. ```APIDOC validate_attributes() Description: Check that the attributes of a drawing are consistent. ``` ```Python from discopy.monoidal import Ty, Id x = Ty('x') drawing = Id(x).to_drawing() drawing.add_edges([(Node("cod", i=0, x=x), Node("dom", i=0, x=x))]) drawing.validate_attributes() # Expected output: # Traceback (most recent call last): # ... # ValueError: Wrong edge Node('cod', i=0, x=x) -> Node('dom', i=0, x=x) ``` -------------------------------- ### Python Example: Deserializing discopy.cat.Ob Object from Dictionary Source: https://docs.discopy.org/en/main/main/_api/discopy.cat.Ob This example demonstrates how to use the `from_tree()` class method to reconstruct an `Ob` object from its dictionary serialization. It verifies that the deserialized object is identical to the original. ```python x = Ob('x') assert Ob.from_tree(x.to_tree()) == x ``` -------------------------------- ### DisCoPy Quantum Module API Overview Source: https://docs.discopy.org/en/main/main/_api/discopy.pivotal.Cap Lists classes and functions within the `discopy.quantum` module, covering gates, ansatze, ZX diagrams, and integrations with `tk` and `pennylane`. ```APIDOC discopy.quantum.gates: - Rotation - Rx - Ry - Rz - CU1 - CRz - CRx - Scalar - MixedScalar - Sqrt - sqrt - scalar discopy.quantum.ansatze: - IQPansatz - Sim14ansatz - Sim15ansatz discopy.quantum.zx: - Diagram - Box - Swap - Spider - Z - Y - X - Scalar discopy.quantum.tk: - Circuit - to_tk - from_tk discopy.quantum.pennylane: - PennyLaneCircuit - tk_op_to_pennylane - extract_ops_from_tk - get_post_selection_dict - to_pennylane ``` -------------------------------- ### Python Example: Unfusing Spiders with Diagram.unfuse() Source: https://docs.discopy.org/en/main/main/_api/discopy.frobenius.Diagram Demonstrates how to use the `unfuse()` method of the `Diagram` class to transform a spider diagram into a diagram with one or three legs. This example uses `discopy.drawing.Equation` to visualize the transformation. ```python from discopy.drawing import Equation spider = Spider(3, 5, Ty(''), "$\phi$") @ Ty() Spider.color = "red" Equation(spider, spider.unfuse(), symbol="$\mapsto$").draw( path='docs/_static/hypergraph/unfuse.png') ``` -------------------------------- ### DisCoPy Compact Category Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Swap API documentation for the `discopy.compact` module, which includes structures and operations for compact categories. ```APIDOC discopy.compact: - Diagram ``` -------------------------------- ### Example: Compute Gradient of DisCoPy Circuit Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Circuit This example demonstrates how to use the `grad` method to compute the symbolic gradient of a DisCoPy circuit with respect to a `sympy.Symbol`. It shows how the method decomposes the gradient into a sum of circuits. ```python from math import pi from sympy.abc import phi from discopy.quantum import * circuit = Rz(phi / 2) @ Rz(phi + 1) >> CX assert circuit.grad(phi, mixed=False) == (scalar(pi/2) @ Rz(phi/2 + .5) @ Rz(phi + 1) >> CX) + (Rz(phi / 2) @ scalar(pi) @ Rz(phi + 1.5) >> CX) ``` -------------------------------- ### Example: Checking Hypergraph bijectivity Source: https://docs.discopy.org/en/main/main/_api/discopy.hypergraph.Hypergraph Provides examples of checking the `is_bijective` property for various `Hypergraph` constructions, including simple boxes, transposed boxes, cups, caps, and spiders with different arities, asserting their expected bijectivity status. ```python from discopy.frobenius import Ty, Box, Hypergraph as H x, y = map(Ty, "xy") f = Box('f', x, y).to_hypergraph() assert f.is_bijective and f.transpose().is_bijective assert H.cups(x, x).is_bijective and H.caps(x, x).is_bijective assert H.spiders(0, 0, x).is_bijective assert not H.spiders(1, 2, x).is_bijective ``` -------------------------------- ### DisCoPy Quantum Modules Overview Source: https://docs.discopy.org/en/main/main/_api/discopy.grammar.thue.Rule Overview of the `discopy.quantum` package, which contains various sub-modules for quantum computing concepts, including channels, circuits, and quantum gates. ```APIDOC discopy.quantum: channel circuit gates ``` -------------------------------- ### DisCoPy Library Modules and Classes API Overview Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.index2bitstring Overview of the DisCoPy library's modules, classes, and functions, including quantum gates, ansatze, ZX diagrams, TK and PennyLane integrations, grammar structures (Thue, CFG, Categorial, Pregroup, Dependency), and drawing utilities. ```APIDOC discopy.quantum: gates: - Rotation - Rx - Ry - Rz - CU1 - CRz - CRx - Scalar - MixedScalar - Sqrt - sqrt - scalar ansatze: - IQPansatz - Sim14ansatz - Sim15ansatz zx: - Diagram - Box - Swap - Spider - Z - Y - X - Scalar tk: - Circuit - to_tk - from_tk pennylane: - PennyLaneCircuit - tk_op_to_pennylane - extract_ops_from_tk - get_post_selection_dict - to_pennylane discopy.grammar: thue: - Word - Rule cfg: - Tree - Rule - Word - Id - Operad - Algebra categorial: - Diagram - Box - Word - FA - BA - FC - BC - FX - BX - Functor - cat2ty - tree2diagram pregroup: - Diagram - Box - Cup - Cap - Swap - Word - Category - Functor - eager_parse - brute_force dependency: - from_spacy discopy.drawing: drawing: - Point - PlaneGraph - Drawing - Equation backend: - draw - Backend - TikZ - Matplotlib ``` -------------------------------- ### discopy.closed.Ty Class Definition and Exponentiation Examples Source: https://docs.discopy.org/en/main/main/_api/discopy.closed.Ty Defines the `Ty` class in `discopy.closed`, a monoidal type that supports exponentiation. It inherits from `discopy.monoidal.Ty` and accepts `Ty` objects as `inside` parameters. Includes examples of type exponentiation using `**`, `>>`, and `<<` operators. ```APIDOC class discopy.closed.Ty(*inside) Bases: discopy.monoidal.Ty Description: A closed type is a monoidal type that can be exponentiated. Parameters: inside (Ty): The objects inside the type. ``` ```python x, y, z = Ty(*"xyz") print((x ** y) ** z) ``` ```python print((x >> y) << z) ``` -------------------------------- ### Python Example: Serializing DisCoPy Object to JSON Source: https://docs.discopy.org/en/main/main/_api/discopy.cat.dumps This example demonstrates how to use `discopy.cat.dumps` to serialize a DisCoPy `Box` object, including its daggered form, into a formatted JSON string. It shows the resulting JSON structure for a simple DisCoPy arrow. ```python >>> from discopy.cat import Box, Id >>> f = Box('f', 'x', 'y', data=42) >>> print(dumps(f[::-1] >> Id('x'), indent=4)) { "factory": "cat.Arrow", "inside": [ { "factory": "cat.Box", "name": "f", "dom": { "factory": "cat.Ob", "name": "y" }, "cod": { "factory": "cat.Ob", "name": "x" }, "is_dagger": true, "data": 42 } ], "dom": { "factory": "cat.Ob", "name": "y" }, "cod": { "factory": "cat.Ob", "name": "x" } } ``` -------------------------------- ### DisCoPy Quantum Computing API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.grammar.pregroup.Cup Comprehensive API reference for the `discopy.quantum` package, providing tools for quantum computing, including quantum channels, circuit construction, and various quantum gates. ```APIDOC discopy.quantum: discopy.quantum.channel: - CQ - Channel - Functor - C - Q discopy.quantum.circuit: - Ob - Digit - Qudit - Ty - Circuit - Box - Sum - Swap - Functor - index2bitstring - bitstring2index discopy.quantum.gates: - SelfConjugate - AntiConjugate - Discard - MixedState - Measure - Encode - QuantumGate - ClassicalGate - Copy - Match - Digits - Bits - Ket - Bra - Controlled ``` -------------------------------- ### Python Example: Using discopy.cat.Sum for Arrow Operations Source: https://docs.discopy.org/en/main/main/_api/discopy.cat.Sum Demonstrates how to create and manipulate `Sum` objects in DisCoPy. This example illustrates the creation of a unit sum and verifies the distributive property of composition over sum, as well as the identity property when adding a unit sum. ```python x, y, z = Ob('x'), Ob('y'), Ob('z') f, g = Box('f', x, y), Box('g', y, z) unit = Sum((), x, y) assert f + unit == f == unit + f assert f >> (g + g) == (f >> g) + (f >> g) == (f + f) >> g ``` -------------------------------- ### DisCoPy Quantum Module API Components Source: https://docs.discopy.org/en/main/main/_api/discopy.grammar.categorial.FA Overview of classes and functions available in the `discopy.quantum` module, including gates, ansatze, ZX calculus, and integrations with TKET and PennyLane. ```APIDOC discopy.quantum: gates: - Parametrized - Rotation - Rx - Ry - Rz - CU1 - CRz - CRx - Scalar - MixedScalar - Sqrt - sqrt - scalar ansatze: - IQPansatz - Sim14ansatz - Sim15ansatz zx: - Diagram - Box - Swap - Spider - Z - Y - X - Scalar tk: - Circuit - to_tk - from_tk pennylane: - PennyLaneCircuit - tk_op_to_pennylane - extract_ops_from_tk - get_post_selection_dict - to_pennylane ``` -------------------------------- ### Python Example: Creating and Drawing an Equation Source: https://docs.discopy.org/en/main/main/_api/discopy.drawing.drawing.Equation This example demonstrates how to create `Equation` objects using `discopy.tensor` components like `Spider`, `Swap`, `Dim`, and `Id`. It shows the construction of simple and complex equations, and then how to draw them to a specified file path with custom drawing parameters. ```python from discopy.tensor import Spider, Swap, Dim, Id dim = Dim(2) mu, eta = Spider(2, 1, dim), Spider(0, 1, dim) delta, upsilon = Spider(1, 2, dim), Spider(1, 0, dim) special = Equation(mu >> delta, Id(dim)) frobenius = Equation( delta @ Id(dim) >> Id(dim) @ mu, mu >> delta, Id(dim) @ delta >> mu @ Id(dim)) Equation(special, frobenius, symbol=', ').draw( aspect='equal', wire_labels=False, path='docs/_static/drawing/frobenius-axioms.png') ``` -------------------------------- ### DisCoPy Quantum Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.pennylane.tk_op_to_pennylane API documentation for the `discopy.quantum` module, which serves as a parent for various quantum computing related sub-modules. ```APIDOC discopy.quantum: channel circuit gates ``` -------------------------------- ### DisCoPy Quantum Circuit Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.pennylane.tk_op_to_pennylane API documentation for the `discopy.quantum.circuit` module, providing classes and functions for building and manipulating quantum circuits. ```APIDOC discopy.quantum.circuit: Ob Digit Qudit Ty Circuit Box Sum Swap Functor index2bitstring bitstring2index ``` -------------------------------- ### Python Example: Using Parametrized with SymPy Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.gates.Parametrized This Python example demonstrates the usage of a `Parametrized` gate (specifically `Rz`) within `discopy` in conjunction with `sympy` for symbolic computation. It shows how to access the gate's array representation and how to compose and then `lambdify` a circuit containing symbolic parameters for numerical evaluation. ```python from sympy.abc import phi from sympy import pi, exp, I assert Rz(phi).array[0,0] == exp(-1.0 * I * pi * phi) c = Rz(phi) >> Rz(-phi) assert c.lambdify(phi)(.25) == Rz(.25) >> Rz(-.25) ``` -------------------------------- ### Example of Casting discopy.matrix.Matrix dtype Source: https://docs.discopy.org/en/main/main/_api/discopy.matrix.Matrix Demonstrates how to use the `cast` method to change the data type of a Matrix instance. ```python >>> assert Matrix.id().cast(bool) == Matrix[bool].id() ``` -------------------------------- ### discopy.quantum Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.python.function API documentation for the `discopy.quantum` module, detailing quantum gates, ansatze, ZX calculus diagrams, and integrations with TKET and PennyLane for quantum circuit manipulation. ```APIDOC discopy.quantum: gates: - Parametrized - Rotation - Rx - Ry - Rz - CU1 - CRz - CRx - Scalar - MixedScalar - Sqrt - sqrt - scalar ansatze: - IQPansatz - Sim14ansatz - Sim15ansatz zx: - Diagram - Box - Swap - Spider - Z - Y - X - Scalar tk: - Circuit - to_tk - from_tk pennylane: - PennyLaneCircuit - tk_op_to_pennylane - extract_ops_from_tk - get_post_selection_dict - to_pennylane ``` -------------------------------- ### Python Example: Instantiating discopy.cat.Category Source: https://docs.discopy.org/en/main/main/_api/discopy.cat.Category Demonstrates how to create instances of the `Category` class and shows the default and predefined `CAT` categories. ```python >>> Category() Category(cat.Ob, cat.Arrow) >>> CAT Category(cat.Category, cat.Functor) ``` -------------------------------- ### Convert Discopy Circuit with Bra and Post-selection to PyTket Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Circuit This example shows converting a Discopy circuit that includes a `Bra` gate, leading to post-selection in the `pytket` representation. It demonstrates how `Bra` gates result in `Measure` and `post_select` attributes in the `pytket.Circuit`. ```python >>> circuit3 = Ket(0, 0)\ ... >> H @ qubit\ ... >> qubit @ X\ ... >> CX\ ... >> qubit @ Bra(0) >>> print(repr(circuit3.to_tk())) tk.Circuit(2, 1).H(0).X(1).CX(0, 1).Measure(1, 0).post_select({0: 0}) ``` -------------------------------- ### DisCoPy Quantum, Grammar, and Drawing API Components Overview Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.circuit.Functor A comprehensive list of classes and functions available within the `discopy.quantum`, `discopy.grammar`, and `discopy.drawing` modules, providing an overview of the library's structured API elements and their organization. ```APIDOC - [Rotation](discopy.quantum.gates.Rotation.html) - [Rx](discopy.quantum.gates.Rx.html) - [Ry](discopy.quantum.gates.Ry.html) - [Rz](discopy.quantum.gates.Rz.html) - [CU1](discopy.quantum.gates.CU1.html) - [CRz](discopy.quantum.gates.CRz.html) - [CRx](discopy.quantum.gates.CRx.html) - [Scalar](discopy.quantum.gates.Scalar.html) - [MixedScalar](discopy.quantum.gates.MixedScalar.html) - [Sqrt](discopy.quantum.gates.Sqrt.html) - [sqrt](discopy.quantum.gates.sqrt.html) - [scalar](discopy.quantum.gates.scalar.html) + [ansatze](discopy.quantum.ansatze.html) - [IQPansatz](discopy.quantum.ansatze.IQPansatz.html) - [Sim14ansatz](discopy.quantum.ansatze.Sim14ansatz.html) - [Sim15ansatz](discopy.quantum.ansatze.Sim15ansatz.html) + [zx](discopy.quantum.zx.html) - [Diagram](discopy.quantum.zx.Diagram.html) - [Box](discopy.quantum.zx.Box.html) - [Swap](discopy.quantum.zx.Swap.html) - [Spider](discopy.quantum.zx.Spider.html) - [Z](discopy.quantum.zx.Z.html) - [Y](discopy.quantum.zx.Y.html) - [X](discopy.quantum.zx.X.html) - [Scalar](discopy.quantum.zx.Scalar.html) + [discopy.quantum.tk](discopy.quantum.tk.html) - [Circuit](discopy.quantum.tk.Circuit.html) - [to_tk](discopy.quantum.tk.to_tk.html) - [from_tk](discopy.quantum.tk.from_tk.html) + [discopy.quantum.pennylane](discopy.quantum.pennylane.html) - [PennyLaneCircuit](discopy.quantum.pennylane.PennyLaneCircuit.html) - [tk_op_to_pennylane](discopy.quantum.pennylane.tk_op_to_pennylane.html) - [extract_ops_from_tk](discopy.quantum.pennylane.extract_ops_from_tk.html) - [get_post_selection_dict](discopy.quantum.pennylane.get_post_selection_dict.html) - [to_pennylane](discopy.quantum.pennylane.to_pennylane.html) * [grammar](../api/grammar.html) + [thue](discopy.grammar.thue.html) - [Word](discopy.grammar.thue.Word.html) - [Rule](discopy.grammar.thue.Rule.html) + [cfg](discopy.grammar.cfg.html) - [Tree](discopy.grammar.cfg.Tree.html) - [Rule](discopy.grammar.cfg.Rule.html) - [Word](discopy.grammar.cfg.Word.html) - [Id](discopy.grammar.cfg.Id.html) - [Operad](discopy.grammar.cfg.Operad.html) - [Algebra](discopy.grammar.cfg.Algebra.html) + [categorial](discopy.grammar.categorial.html) - [Diagram](discopy.grammar.categorial.Diagram.html) - [Box](discopy.grammar.categorial.Box.html) - [Word](discopy.grammar.categorial.Word.html) - [FA](discopy.grammar.categorial.FA.html) - [BA](discopy.grammar.categorial.BA.html) - [FC](discopy.grammar.categorial.FC.html) - [BC](discopy.grammar.categorial.BC.html) - [FX](discopy.grammar.categorial.FX.html) - [BX](discopy.grammar.categorial.BX.html) - [Functor](discopy.grammar.categorial.Functor.html) - [cat2ty](discopy.grammar.categorial.cat2ty.html) - [tree2diagram](discopy.grammar.categorial.tree2diagram.html) + [pregroup](discopy.grammar.pregroup.html) - [Diagram](discopy.grammar.pregroup.Diagram.html) - [Box](discopy.grammar.pregroup.Box.html) - [Cup](discopy.grammar.pregroup.Cup.html) - [Cap](discopy.grammar.pregroup.Cap.html) - [Swap](discopy.grammar.pregroup.Swap.html) - [Word](discopy.grammar.pregroup.Word.html) - [Category](discopy.grammar.pregroup.Category.html) - [Functor](discopy.grammar.pregroup.Functor.html) - [eager_parse](discopy.grammar.pregroup.eager_parse.html) - [brute_force](discopy.grammar.pregroup.brute_force.html) + [dependency](discopy.grammar.dependency.html) - [from_spacy](discopy.grammar.dependency.from_spacy.html) * [drawing](../api/drawing.html) + [drawing](discopy.drawing.drawing.html) - [Point](discopy.drawing.drawing.Point.html) - [PlaneGraph](discopy.drawing.drawing.PlaneGraph.html) - [Drawing](discopy.drawing.drawing.Drawing.html) - [Equation](discopy.drawing.drawing.Equation.html) + [backend](discopy.drawing.backend.html) - [draw](discopy.drawing.backend.draw.html) - [Backend](discopy.drawing.backend.Backend.html) - [TikZ](discopy.drawing.backend.TikZ.html) - [Matplotlib](discopy.drawing.backend.Matplotlib.html) ``` -------------------------------- ### Example: Accessing Hypergraph bijection Source: https://docs.discopy.org/en/main/main/_api/discopy.hypergraph.Hypergraph Demonstrates how to access the `bijection` property of a `Hypergraph` and iterate through its ports and their corresponding bijective mappings. ```python from discopy.frobenius import Ty, Box, Hypergraph as H x, y = map(Ty, "xy") f = Box('f', x, y).to_hypergraph() for i, port in enumerate(f.ports): print(i, port) for i, j in enumerate(f.bijection): print(f"{i} -> {j}") ``` -------------------------------- ### discopy.grammar.cfg Module API Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.channel API documentation for the discopy.grammar.cfg module, including classes for Context-Free Grammar components such as Tree, Rule, and Word. ```APIDOC discopy.grammar.cfg: Tree Rule Word Id Operad Algebra ``` -------------------------------- ### Simplify DisCoPy Diagram with Foliation Source: https://docs.discopy.org/en/main/main/_api/discopy.monoidal.Diagram This example demonstrates how the `foliation` method merges layers to reduce the length of a diagram, showing a more compact representation. ```python from discopy.monoidal import * x, y = Ty('x'), Ty('y') f0, f1 = Box('f0', x, y), Box('f1', y, x) diagram = f0 @ f1.dagger() >> f0.dagger() @ f1 print(diagram) diagram.foliation().draw( path='docs/_static/monoidal/foliation-example.png') ``` -------------------------------- ### DisCoPy Python Finset Module API Reference Source: https://docs.discopy.org/en/main/main/_api/discopy.quantum.pennylane.tk_op_to_pennylane API documentation for the `discopy.python.finset` module, including functionalities for finite sets. ```APIDOC discopy.python.finset: Function ```