Circuit#
- class discopy.quantum.circuit.Circuit(dom, cod, boxes, offsets, layers=None)[source]#
Bases:
DiagramClassical-quantum circuits.
- property is_mixed#
Whether the circuit is mixed, i.e. it contains both bits and qubits or it discards qubits. Mixed circuits can be evaluated only by a
CQMapFunctornot adiscopy.tensor.Functor.
- eval(*others, backend=None, mixed=False, contractor=None, **params)[source]#
Evaluate a circuit on a backend, or simulate it with numpy.
- Parameters:
others (
discopy.quantum.circuit.Circuit) – Other circuits to process in batch.backend (pytket.Backend, optional) – Backend on which to run the circuit, if none then we apply
discopy.tensor.FunctororCQMapFunctorinstead.mixed (bool, optional) – Whether to apply
discopy.tensor.FunctororCQMapFunctor.contractor (callable, optional) – Use
tensornetworkcontraction instead of discopy’s basic eval feature.params (kwargs, optional) – Get passed to Circuit.get_counts.
- Returns:
tensor (
discopy.tensor.Tensor) – Ifbackend is not Noneormixed=False.cqmap (
CQMap) – Otherwise.
Examples
We can evaluate a pure circuit (i.e. with
not circuit.is_mixed) as a unitarydiscopy.tensor.Tensoror as aCQMap:>>> from discopy.quantum import *
>>> H.eval().round(2) Tensor(dom=Dim(2), cod=Dim(2), array=[0.71+0.j, ..., -0.71+0.j]) >>> H.eval(mixed=True).round(1) CQMap(dom=Q(Dim(2)), cod=Q(Dim(2)), array=[0.5+0.j, ..., 0.5+0.j])
We can evaluate a mixed circuit as a
CQMap:>>> assert Measure().eval()\ ... == CQMap(dom=Q(Dim(2)), cod=C(Dim(2)), ... array=[1, 0, 0, 0, 0, 0, 0, 1]) >>> circuit = Bits(1, 0) @ Ket(0) >> Discard(bit ** 2 @ qubit) >>> assert circuit.eval() == CQMap(dom=CQ(), cod=CQ(), array=[1])
We can execute any circuit on a pytket.Backend:
>>> 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}) >>> assert circuit.eval(backend, n_shots=2**10).round()\ ... == Tensor(dom=Dim(1), cod=Dim(2), array=[0., 1.])
- get_counts(*others, backend=None, **params)[source]#
Get counts from a backend, or simulate them with numpy.
- Parameters:
others (
discopy.quantum.circuit.Circuit) – Other circuits to process in batch.backend (pytket.Backend, optional) – Backend on which to run the circuit, if none then numpy.
n_shots (int, optional) – Number of shots, default is
2**10.measure_all (bool, optional) – Whether to measure all qubits, default is
False.normalize (bool, optional) – Whether to normalize the counts, default is
True.post_select (bool, optional) – Whether to perform post-selection, default is
True.scale (bool, optional) – Whether to scale the output, default is
True.seed (int, optional) – Seed to feed the backend, default is
None.compilation (callable, optional) – Compilation function to apply before getting counts.
- Returns:
counts – From bitstrings to counts.
- Return type:
dict
Examples
>>> 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, n_shots=2**10) {(0, 1): 0.5, (1, 0): 0.5}
- measure(mixed=False)[source]#
Measure a circuit on the computational basis using
numpy.- Parameters:
mixed (bool, optional) – Whether to apply
tensor.Functororcqmap.Functor.- Returns:
array
- Return type:
numpy.ndarray
- to_tn(mixed=False)[source]#
Send a diagram to a mixed
tensornetwork.- Parameters:
mixed (bool, default: False) – Whether to perform mixed (also known as density matrix) evaluation of the circuit.
- Returns:
nodes (
tensornetwork.Node) – Nodes of the network.output_edge_order (list of
tensornetwork.Edge) – Output edges of the network.
- to_tk()[source]#
Export to t|ket>.
- Returns:
tk_circuit – A
pytket.Circuit.- Return type:
pytket.Circuit
Note
No measurements are performed.
SWAP gates are treated as logical swaps.
If the circuit contains scalars or a
Bra, thentk_circuitwill hold attributespost_selectionandscalar.
Examples
>>> from discopy.quantum import *
>>> bell_test = H @ Id(1) >> CX >> Measure() @ Measure() >>> bell_test.to_tk() tk.Circuit(2, 2).H(0).CX(0, 1).Measure(0, 0).Measure(1, 1)
>>> circuit0 = sqrt(2) @ H @ Rx(0.5) >> CX >> Measure() @ Discard() >>> circuit0.to_tk() tk.Circuit(2, 1).H(0).Rx(1.0, 1).CX(0, 1).Measure(0, 0).scale(2)
>>> circuit1 = Ket(1, 0) >> CX >> Id(1) @ Ket(0) @ Id(1) >>> circuit1.to_tk() tk.Circuit(3).X(0).CX(0, 2)
>>> circuit2 = X @ Id(2) >> Id(1) @ SWAP >> CX @ Id(1) >> Id(1) @ SWAP >>> circuit2.to_tk() tk.Circuit(3).X(0).CX(0, 2)
>>> circuit3 = Ket(0, 0)\ ... >> H @ Id(1)\ ... >> Id(1) @ X\ ... >> CX\ ... >> Id(1) @ 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})
- to_pennylane(probabilities=False, backend_config=None, diff_method='best')[source]#
Export DisCoPy circuit to PennylaneCircuit.
- Parameters:
probabilties (bool, default: False) – If True, the PennylaneCircuit will return the normalized probabilties of measuring the computational basis states when run. If False, it returns the unnormalized quantum states in the computational basis.
- Return type:
discopy.quantum.pennylane.PennylaneCircuit
- static from_tk(*tk_circuits)[source]#
Translate a
pytket.Circuitinto aCircuit, or a list ofpytketcircuits into aSum.- Parameters:
tk_circuits (pytket.Circuit) – potentially with
scalarandpost_selectionattributes.- Returns:
circuit – Such that
Circuit.from_tk(circuit.to_tk()) == circuit.- Return type:
Note
Circuit.init_and_discard()is applied beforehand.SWAP gates are introduced when applying gates to non-adjacent qubits.
Examples
>>> from discopy.quantum import * >>> import pytket as tk
>>> c = Rz(0.5) @ Id(1) >> Id(1) @ Rx(0.25) >> CX >>> assert Circuit.from_tk(c.to_tk()) == c.init_and_discard()
>>> 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)) Ket(0) >> Id(1) @ Ket(0) >> Id(2) @ Ket(0) >> Id(1) @ H @ Id(1) >> Id(1) @ CX >> SWAP @ Id(1) >> CX @ Id(1) >> SWAP @ Id(1) >> Discard(qubit) @ Id(2) >> Discard(qubit) @ Id(1) >> Discard(qubit) >>> circuit = Ket(1, 0) >> CX >> Id(1) @ Ket(0) @ Id(1) >>> print(Circuit.from_tk(circuit.to_tk())[3:-3]) X @ Id(2) >> Id(1) @ SWAP >> CX @ Id(1) >> Id(1) @ SWAP
>>> bell_state = Circuit.caps(qubit, qubit) >>> bell_effect = bell_state[::-1] >>> circuit = bell_state @ Id(1) >> Id(1) @ bell_effect >> Bra(0) >>> pprint(Circuit.from_tk(circuit.to_tk())[3:]) H @ Id(2) >> CX @ Id(1) >> Id(1) @ CX >> Id(1) @ H @ Id(1) >> Bra(0) @ Id(2) >> Bra(0) @ Id(1) >> Bra(0) >> scalar(4)
- grad(var, **params)[source]#
Gradient with respect to
var.- Parameters:
var (sympy.Symbol) – Differentiated variable.
- Returns:
circuit
- Return type:
discopy.quantum.circuit.Sum
Examples
>>> from sympy.abc import phi >>> from discopy.quantum import * >>> circuit = Rz(phi / 2) @ Rz(phi + 1) >> CX >>> assert circuit.grad(phi, mixed=False)\ ... == (Rz(phi / 2) @ scalar(pi) @ Rz(phi + 1.5) >> CX)\ ... + (scalar(pi/2) @ Rz(phi/2 + .5) @ Rz(phi + 1) >> CX)
- jacobian(variables, **params)[source]#
Jacobian with respect to
variables.- Parameters:
variables (List[sympy.Symbol]) – Differentiated variables.
- Returns:
circuit – with
circuit.dom == self.domandcircuit.cod == Digit(len(variables)) @ self.cod.- Return type:
discopy.quantum.circuit.Sum
Examples
>>> from sympy.abc import x, y >>> from discopy.quantum.gates import Bits, Ket, Rx, Rz >>> circuit = Ket(0) >> Rx(x) >> Rz(y) >>> assert circuit.jacobian([x, y])\ ... == (Bits(0) @ circuit.grad(x)) + (Bits(1) @ circuit.grad(y)) >>> assert not circuit.jacobian([]) >>> assert circuit.jacobian([x]) == circuit.grad(x)