
    z	i                    `    S r SSKJr  SSKrSSKJr  SSKJr  SSK	J
r
   " S S\5      rS
S	 jrg)zHidden Linear Function circuit.    )annotationsN)QuantumCircuit)CircuitError)deprecate_funcc                  F   ^  \ rS rSrSr\" SSSS9S	U 4S jj5       rSrU =r$ )
HiddenLinearFunction   a&  Circuit to solve the hidden linear function problem.

The 2D Hidden Linear Function problem is determined by a 2D adjacency
matrix A, where only elements that are nearest-neighbor on a grid have
non-zero entries. Each row/column corresponds to one binary variable
:math:`x_i`.

The hidden linear function problem is as follows:

Consider the quadratic form

.. math::

    q(x) = \sum_{i,j=1}^{n}{x_i x_j} ~(\mathrm{mod}~ 4)

and restrict :math:`q(x)` onto the nullspace of A. This results in a linear
function.

.. math::

    2 \sum_{i=1}^{n}{z_i x_i} ~(\mathrm{mod}~ 4)  \forall  x \in \mathrm{Ker}(A)

and the goal is to recover this linear function (equivalently a vector
:math:`[z_0, ..., z_{n-1}]`). There can be multiple solutions.

In [1] it is shown that the present circuit solves this problem
on a quantum computer in constant depth, whereas any corresponding
solution on a classical computer would require circuits that grow
logarithmically with :math:`n`. Thus this circuit is an example
of quantum advantage with shallow circuits.

Reference Circuit:

.. plot::
    :alt: Diagram illustrating the previously described circuit.

    from qiskit.circuit.library import HiddenLinearFunction
    from qiskit.visualization.library import _generate_circuit_library_visualization
    A = [[1, 1, 0], [1, 0, 1], [0, 1, 1]]
    circuit = HiddenLinearFunction(A)
    _generate_circuit_library_visualization(circuit)

References:

[1] S. Bravyi, D. Gosset, R. Koenig, Quantum Advantage with Shallow Circuits, 2017.
`arXiv:1704.00690 <https://arxiv.org/abs/1704.00690>`_
z2.1z:Use qiskit.circuit.library.hidden_linear_function instead.zin Qiskit 3.0)sinceadditional_msgremoval_timelinec                   > [        U5      n[        TU ]  " UR                  SUR                  06  U R                  UR                  5       U R                  5        g)zCreate new HLF circuit.

Args:
    adjacency_matrix: a symmetric n-by-n list of 0-1 lists.
        n will be the number of qubits.

Raises:
    CircuitError: If A is not symmetric.
nameN)hidden_linear_functionsuper__init__qregsr   appendto_gatequbits)selfadjacency_matrixcircuit	__class__s      g/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/library/hidden_linear_function.pyr   HiddenLinearFunction.__init__H   sC     ))9:'--;gll;GOO%t{{3     )r   list | np.ndarrayreturnNone)	__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes____classcell__)r   s   @r   r   r      s+    .` S(
4
4r   r   c                "   [         R                  " U 5      n [         R                  " X R                  5       5      (       d  [	        S5      e[        U 5      n[        USU  3S9nUR                  [        U5      5        [        U5       H8  n[        US-   U5       H"  nX   U   (       d  M  UR                  X45        M$     M:     [        U5       H"  nX   U   (       d  M  UR                  U5        M$     UR                  [        U5      5        U$ )aY  Circuit to solve the hidden linear function problem.

The 2D Hidden Linear Function problem is determined by a 2D adjacency
matrix A, where only elements that are nearest-neighbor on a grid have
non-zero entries. Each row/column corresponds to one binary variable
:math:`x_i`.

The hidden linear function problem is as follows:

Consider the quadratic form

.. math::

    q(x) = \sum_{i,j=1}^{n}{x_i x_j} ~(\mathrm{mod}~ 4)

and restrict :math:`q(x)` onto the nullspace of A. This results in a linear
function.

.. math::

    2 \sum_{i=1}^{n}{z_i x_i} ~(\mathrm{mod}~ 4)  \forall  x \in \mathrm{Ker}(A)

and the goal is to recover this linear function (equivalently a vector
:math:`[z_0, ..., z_{n-1}]`). There can be multiple solutions.

In [1] it is shown that the present circuit solves this problem
on a quantum computer in constant depth, whereas any corresponding
solution on a classical computer would require circuits that grow
logarithmically with :math:`n`. Thus this circuit is an example
of quantum advantage with shallow circuits.

Reference Circuit:

.. plot::
   :alt: Circuit diagram output by the previous code.
   :include-source:

   from qiskit.circuit.library import hidden_linear_function
   A = [[1, 1, 0], [1, 0, 1], [0, 1, 1]]
   circuit = hidden_linear_function(A)
   circuit.draw('mpl')

Args:
    adjacency_matrix: a symmetric n-by-n list of 0-1 lists.
        n will be the number of qubits.

Raises:
    CircuitError: If A is not symmetric.

Reference:

[1] S. Bravyi, D. Gosset, R. Koenig, Quantum Advantage with Shallow Circuits, 2017.
`arXiv:1704.00690 <https://arxiv.org/abs/1704.00690>`_
z'The adjacency matrix must be symmetric.zhlf: )r      )npasarrayallclose	transposer   lenr   hrangeczs)r   
num_qubitsr   ijs        r   r   r   \   s    n zz"23;;')C)C)EFFDEE%&JZ6F5G.HIGIIeJ :q1uj)A"1%%

1  *  :q!!IIaL  IIeJ Nr   )r   r   r   r   )r%   
__future__r   numpyr*   qiskit.circuit.quantumcircuitr   qiskit.circuit.exceptionsr   qiskit.utils.deprecationr   r   r   r   r   r   <module>r;      s.    & "  8 2 3B4> B4JGr   