class_docstr: Prepares an arbitrary state on the given wires using a decomposition into gates developed
  by `Möttönen et al. (2004) <https://arxiv.org/abs/quant-ph/0407010>`_.
  
  The state is prepared via a sequence
  of uniformly controlled rotations. A uniformly controlled rotation on a target qubit is
  composed from all possible controlled rotations on the qubit and can be used to address individual
  elements of the state vector.
  
  In the work of Möttönen et al., inverse state preparation
  is executed by first equalizing the phases of the state vector via uniformly controlled Z rotations,
  and then rotating the now real state vector into the direction of the state :math:`|0\rangle` via
  uniformly controlled Y rotations.
  
  This code is adapted from code written by Carsten Blank for PennyLane-Qiskit.
  
  .. note::
  
      The final state is only equal to the input state vector up to a global phase.
  
  .. warning::
  
      Due to non-trivial classical processing of the state vector,
      this template is not always fully differentiable.
  
  Args:
      state_vector (tensor_like): Input array of shape ``(2^n,)``, where ``n`` is the number of wires
          the state preparation acts on. The input array must be normalized.
      wires (Iterable): wires that the template acts on
  
  Example:
  
      ``MottonenStatePreparation`` creates any arbitrary state on the given wires depending on the input state vector.
  
      .. code-block:: python
  
          dev = qml.device('default.qubit', wires=3)
  
          @qml.qnode(dev)
          def circuit(state):
              qml.MottonenStatePreparation(state_vector=state, wires=range(3))
              return qml.state()
  
          state = np.array([1, 2j, 3, 4j, 5, 6j, 7, 8j])
          state = state / np.linalg.norm(state)
  
          print(qml.draw(circuit, expansion_strategy="device", max_length=80)(state))
  
      .. code-block::
  
          0: ──RY(2.35)─╭●───────────╭●──────────────╭●────────────────────────╭●
          1: ──RY(2.09)─╰X──RY(0.21)─╰X─╭●───────────│────────────╭●───────────│─
          2: ──RY(1.88)─────────────────╰X──RY(0.10)─╰X──RY(0.08)─╰X──RY(0.15)─╰X
  
          ──╭●────────╭●────╭●────╭●─┤  State
          ──╰X────────╰X─╭●─│──╭●─│──┤  State
          ───RZ(1.57)────╰X─╰X─╰X─╰X─┤  State
  
      The state preparation can be checked by running:
  
      >>> print(np.allclose(state * np.exp(1j * -0.785396), circuit(state)))
          True
  
      The state is equal to the input state upto a global phase. This phase is given by ``np.exp(1j * -0.785396)`` in this example.
class_docstr_tok: ["Prepares","an","arbitrary","state","on","the","given","wires","using","a","decomposition","into","gates","developed","by","`","M\u00f6tt\u00f6nen","et","al",".","(","2004",")","<","https",":","\/\/arxiv.org\/abs\/quant-ph\/0407010",">","`","_",".","The","state","is","prepared","via","a","sequence","of","uniformly","controlled","rotations",".","A","uniformly","controlled","rotation","on","a","target","qubit","is","composed","from","all","possible","controlled","rotations","on","the","qubit","and","can","be","used","to","address","individual","elements","of","the","state","vector",".","In","the","work","of","M\u00f6tt\u00f6nen","et","al.",",","inverse","state","preparation","is","executed","by","first","equalizing","the","phases","of","the","state","vector","via","uniformly","controlled","Z","rotations",",","and","then","rotating","the","now","real","state","vector","into","the","direction","of","the","state",":","math",":","`","|0\\rangle","`","via","uniformly","controlled","Y","rotations",".","This","code","is","adapted","from","code","written","by","Carsten","Blank","for","PennyLane-Qiskit",".","..","note",":",":","The","final","state","is","only","equal","to","the","input","state","vector","up","to","a","global","phase",".","..","warning",":",":","Due","to","non-trivial","classical","processing","of","the","state","vector",",","this","template","is","not","always","fully","differentiable",".","Args",":","state_vector","(","tensor_like",")",":","Input","array","of","shape","``","(","2^n",",",")","``",",","where","``","n","``","is","the","number","of","wires","the","state","preparation","acts","on",".","The","input","array","must","be","normalized",".","wires","(","Iterable",")",":","wires","that","the","template","acts","on","Example",":","``","MottonenStatePreparation","``","creates","any","arbitrary","state","on","the","given","wires","depending","on","the","input","state","vector",".","..","code-block",":",":","python","dev","=","qml.device","(","'default.qubit","'",",","wires=3",")","@","qml.qnode","(","dev",")","def","circuit","(","state",")",":","qml.MottonenStatePreparation","(","state_vector=state",",","wires=range","(","3",")",")","return","qml.state","(",")","state","=","np.array","(","[","1",",","2j",",","3",",","4j",",","5",",","6j",",","7",",","8j","]",")","state","=","state","\/","np.linalg.norm","(","state",")","print","(","qml.draw","(","circuit",",","expansion_strategy=","''","device","''",",","max_length=80",")","(","state",")",")","..","code-block",":",":","0",":","\u2500\u2500RY","(","2.35",")","\u2500\u256d\u25cf\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256d\u25cf\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256d\u25cf\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256d\u25cf","1",":","\u2500\u2500RY","(","2.09",")","\u2500\u2570X\u2500\u2500RY","(","0.21",")","\u2500\u2570X\u2500\u256d\u25cf\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2502\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256d\u25cf\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2502\u2500","2",":","\u2500\u2500RY","(","1.88",")","\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2570X\u2500\u2500RY","(","0.10",")","\u2500\u2570X\u2500\u2500RY","(","0.08",")","\u2500\u2570X\u2500\u2500RY","(","0.15",")","\u2500\u2570X","\u2500\u2500\u256d\u25cf\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256d\u25cf\u2500\u2500\u2500\u2500\u256d\u25cf\u2500\u2500\u2500\u2500\u256d\u25cf\u2500\u2524","State","\u2500\u2500\u2570X\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2570X\u2500\u256d\u25cf\u2500\u2502\u2500\u2500\u256d\u25cf\u2500\u2502\u2500\u2500\u2524","State","\u2500\u2500\u2500RZ","(","1.57",")","\u2500\u2500\u2500\u2500\u2570X\u2500\u2570X\u2500\u2570X\u2500\u2570X\u2500\u2524","State","The","state","preparation","can","be","checked","by","running",":",">",">",">","print","(","np.allclose","(","state","*","np.exp","(","1j","*","-0.785396",")",",","circuit","(","state",")",")",")","True","The","state","is","equal","to","the","input","state","upto","a","global","phase",".","This","phase","is","given","by","``","np.exp","(","1j","*","-0.785396",")","``","in","this","example","."]
code: def __init__(self, state_vector, wires, id=None):
      # check if the `state_vector` param is batched
      batched = len(qml.math.shape(state_vector)) > 1
  
      state_batch = state_vector if batched else [state_vector]
  
      # apply checks to each state vector in the batch
      for i, state in enumerate(state_batch):
          shape = qml.math.shape(state)
  
          if len(shape) != 1:
              raise ValueError(
                  f"State vectors must be one-dimensional; vector {i} has shape {shape}."
              )
  
          n_amplitudes = shape[0]
          if n_amplitudes != 2 ** len(qml.wires.Wires(wires)):
              raise ValueError(
                  f"State vectors must be of length {2 ** len(wires)} or less; vector {i} has length {n_amplitudes}."
              )
  
          if not qml.math.is_abstract(state):
              norm = qml.math.sum(qml.math.abs(state) ** 2)
              if not qml.math.allclose(norm, 1.0, atol=1e-3):
                  raise ValueError(
                      f"State vectors have to be of norm 1.0, vector {i} has norm {norm}"
                  )
  
      super().__init__(state_vector, wires=wires, id=id)
  
file_path: pennylane/pennylane/templates/state_preparations/mottonen.py
imports: import numpy
  import pennylane
  from pennylane.operation import Operation, AnyWires
label_desc: Use image node_id 1 to create a new MottonenStatePreparation object from inherited base classes: Operation with example: obj = MottonenStatePreparation(state_vector, wires, id)