class_docstr: Implements the heuristic VQE ansatz for quantum chemistry simulations using the
  particle-conserving gate :math:`U_{1,\mathrm{ex}}` proposed by Barkoutsos *et al.* in
  `arXiv:1805.04340 <https://arxiv.org/abs/1805.04340>`_.
  
  This template prepares :math:`N`-qubit trial states by applying :math:`D` layers of the
  entangler block :math:`U_\mathrm{ent}(\vec{\phi}, \vec{\theta})` to the Hartree-Fock
  state
  
  .. math::
  
      \vert \Psi(\vec{\phi}, \vec{\theta}) \rangle = \hat{U}^{(D)}_\mathrm{ent}(\vec{\phi}_D,
      \vec{\theta}_D) \dots \hat{U}^{(2)}_\mathrm{ent}(\vec{\phi}_2, \vec{\theta}_2)
      \hat{U}^{(1)}_\mathrm{ent}(\vec{\phi}_1, \vec{\theta}_1) \vert \mathrm{HF}\rangle.
  
  The circuit implementing the entangler blocks is shown in the figure below:
  
  |
  
  .. figure:: ../../_static/templates/layers/particle_conserving_u1.png
      :align: center
      :width: 50%
      :target: javascript:void(0);
  
  |
  
  The repeated units across several qubits are shown in dotted boxes. Each layer
  contains :math:`N-1` particle-conserving two-parameter exchange gates
  :math:`U_{1,\mathrm{ex}}(\phi, \theta)` that act on pairs of nearest neighbors qubits.
  The unitary matrix representing :math:`U_{1,\mathrm{ex}}(\phi, \theta)`
  is given by (see `arXiv:1805.04340 <https://arxiv.org/abs/1805.04340>`_),
  
  .. math::
  
      U_{1, \mathrm{ex}}(\phi, \theta) = \left(\begin{array}{cccc}
      1 & 0 & 0 & 0 \\
      0 & \mathrm{cos}(\theta) & e^{i\phi} \mathrm{sin}(\theta) & 0 \\
      0 & e^{-i\phi} \mathrm{sin}(\theta) & -\mathrm{cos}(\theta) & 0 \\
      0 & 0 & 0 & 1 \\
      \end{array}\right).
  
  The figure below shows the circuit decomposing :math:`U_{1, \mathrm{ex}}` in
  elementary gates. The Pauli matrix :math:`\sigma_z` and single-qubit rotation
  :math:`R(0, 2 \theta, 0)` apply the Pauli Z operator and an arbitrary rotation
  on the qubit ``n`` with qubit ``m`` bein the control qubit,
  
  |
  
  .. figure:: ../../_static/templates/layers/u1_decomposition.png
      :align: center
      :width: 80%
      :target: javascript:void(0);
  
  |
  
  :math:`U_A(\phi)` is the unitary matrix
  
  .. math::
  
      U_A(\phi) = \left(\begin{array}{cc} 0 & e^{-i\phi} \\ e^{-i\phi} & 0 \\ \end{array}\right),
  
  which is applied controlled on the state of qubit ``m`` and can be further decomposed in
  terms of the
  `quantum operations <https://pennylane.readthedocs.io/en/stable/introduction/operations.html>`_
  supported by Pennylane,
  
  |
  
  .. figure:: ../../_static/templates/layers/ua_decomposition.png
      :align: center
      :width: 70%
      :target: javascript:void(0);
  
  |
  
  where,
  
  |
  
  .. figure:: ../../_static/templates/layers/phaseshift_decomposition.png
      :align: center
      :width: 65%
      :target: javascript:void(0);
  
  |
  
  The quantum circuits above decomposing the unitaries :math:`U_{1,\mathrm{ex}}(\phi, \theta)`
  and :math:`U_A(\phi)` are implemented by the ``u1_ex_gate`` and ``decompose_ua``
  functions, respectively. :math:`R_\phi` refers to the ``PhaseShift`` gate in the
  circuit diagram.
  
  Args:
      weights (tensor_like): Array of weights of shape ``(D, M, 2)``.
          ``D`` is the number of entangler block layers and :math:`M=N-1`
          is the number of exchange gates :math:`U_{1,\mathrm{ex}}` per layer.
      wires (Iterable): wires that the template acts on
      init_state (tensor_like): iterable or shape ``(len(wires),)`` tensor representing the Hartree-Fock state
          used to initialize the wires
  
  .. details::
      :title: Usage Details
  
      #. The number of wires :math:`N` has to be equal to the number of
         spin orbitals included in the active space.
  
      #. The number of trainable parameters scales linearly with the number of layers as
         :math:`2D(N-1)`.
  
      An example of how to use this template is shown below:
  
      .. code-block:: python
  
          import pennylane as qml
          import numpy as np
          from functools import partial
  
          # Build the electronic Hamiltonian
          symbols, coordinates = (['H', 'H'], np.array([0., 0., -0.66140414, 0., 0., 0.66140414]))
          h, qubits = qml.qchem.molecular_hamiltonian(symbols, coordinates)
  
          # Define the Hartree-Fock state
          electrons = 2
          ref_state = qml.qchem.hf_state(electrons, qubits)
  
          # Define the device
          dev = qml.device('default.qubit', wires=qubits)
  
          # Define the ansatz
          ansatz = partial(qml.ParticleConservingU1, init_state=ref_state)
  
          # Define the cost function
          cost_fn = qml.ExpvalCost(ansatz, h, dev)
  
          # Compute the expectation value of 'h'
          layers = 2
          shape = qml.ParticleConservingU1.shape(layers, qubits)
          params = np.random.random(shape)
          print(cost_fn(params))
  
      **Parameter shape**
  
      The shape of the trainable weights tensor can be computed by the static method
      :meth:`~.ParticleConservingU1.shape` and used when creating randomly
      initialised weight tensors:
  
      .. code-block:: python
  
          shape = qml.ParticleConservingU1.shape(n_layers=2, n_wires=2)
          params = np.random.random(size=shape)
code: def num_params(self):
      return 1
  
file_path: pennylane/pennylane/templates/layers/particle_conserving_u1.py
imports: import numpy
  import pennylane
  from pennylane.operation import Operation, AnyWires
label_desc: Use image node_id 2 for calling the ParticleConservingU1 obj's underlying member method code with example usage: obj.num_params() and returns: int