tensorcircuit.templates.blocksΒΆ

Shortcut building blocks that compose common circuit structures (Bell pairs, grid entangling layers, QAOA layers, QFT, etc.) on a given circuit.

tensorcircuit.templates.blocks.Bell_pair_block(c: Any, links: Sequence[Tuple[int, int]] | None = None) Any[source]ΒΆ

For each pair in links, the input product state |00> is transformed as (01>-|10>)

Parameters:
  • c (Circuit) – Circuit in

  • links (Optional[Sequence[Tuple[int, int]]], optional) – pairs indices for Bell pairs, defaults to None, corresponds to neighbor links

Returns:

Circuit out

Return type:

Circuit

tensorcircuit.templates.blocks.Grid2D_entangling(c: Any, coord: Grid2DCoord, unitary: Any, params: Any, **kws: Any) Any[source]ΒΆ

Apply a parametrized two-qubit entangling gate (exp1) along every row and column edge of the 2D grid coord.

Parameters:
  • c (Circuit) – circuit to append gates onto

  • coord (Grid2DCoord) – 2D grid coordinate object defining the row/column edges

  • unitary (Tensor) – two-qubit unitary generator used by exp1

  • params (Tensor) – 1D tensor of rotation angles, ordered row-edges then column-edges

Returns:

the same circuit with entangling layers appended

Return type:

Circuit

tensorcircuit.templates.blocks.QAOA_block(c: Any, g: Any, paramzz: Any, paramx: Any, **kws: Any) Any[source]ΒΆ

Append one QAOA layer (cost + mixer) onto c for the graph g.

The cost layer applies exp1(..., _zz_matrix, paramzz) on each edge (scaled by the edge weight if paramzz is scalar), and the mixer layer applies rx(paramx) on each node.

Parameters:
  • c (Circuit) – circuit to append the QAOA layer onto

  • g (Graph) – problem graph whose edges/nodes define the cost and mixer terms

  • paramzz (Tensor) – ZZ rotation angle(s); scalar (broadcast) or per-edge vector

  • paramx (Tensor) – RX rotation angle(s); scalar (broadcast) or per-node vector

Returns:

the same circuit with the QAOA layer appended

Return type:

Circuit

tensorcircuit.templates.blocks.example_block(c: Any, param: Any, nlayers: int = 2, is_split: bool = False) Any[source]ΒΆ

The circuit ansatz is firstly one layer of Hadamard gates and then we have nlayers blocks of \(e^{i\theta Z_iZ_{i+1}}\) two-qubit gate in ladder layout, following rx gate.

Parameters:
  • c (Circuit) – The circuit

  • param (Tensor) – paramter tensor with 2*nlayer*n elements

  • nlayers (int, optional) – number of ZZ+RX blocks, defaults to 2

  • is_split (bool, optional) – whether use SVD split to reduce ZZ gate bond dimension, defaults to False

Returns:

The circuit with example ansatz attached

Return type:

Circuit

tensorcircuit.templates.blocks.qft(c: Any, *index: int, do_swaps: bool = True, inverse: bool = False, insert_barriers: bool = False) Any[source]ΒΆ

This function applies quantum fourier transformation (QFT) to the selected circuit lines

Parameters:
  • c (Circuit) – Circuit in

  • *index –

    the indices of the circuit lines to apply QFT

  • do_swaps (bool) – Whether to include the final swaps in the QFT

  • inverse (bool) – If True, the inverse Fourier transform is constructed

  • insert_barriers (bool) – If True, barriers are inserted as visualization improvement

Returns:

Circuit c

Return type:

Circuit

tensorcircuit.templates.blocks.state_centric(f: Callable[[...], Any]) Callable[[...], Any][source]ΒΆ

Function decorator wraps the function with the first input and output in the format of circuit, the wrapped function has the first input and the output as the state tensor.

Parameters:

f (Callable[..., Circuit]) – Function with the fist input and the output as Circuit object.

Returns:

Wrapped function with the first input and the output as the state tensor correspondingly.

Return type:

Callable[…, Tensor]