class_docstr: The operations and measurements that represent instructions for
  execution on a quantum device.
  
  Args:
      ops (Iterable[Operator]): An iterable of the operations to be performed
      measurements (Iterable[MeasurementProcess]): All the measurements to be performed
  
  Keyword Args:
      shots (None, int, Sequence[int], ~.Shots): Number and/or batches of shots for execution.
          Note that this property is still experimental and under development.
      trainable_params (None, Sequence[int]): the indices for which parameters are trainable
      _update=True (bool): Whether or not to set various properties on initialization. Setting
          ``_update=False`` reduces computations if the script is only an intermediary step.
  
  .. seealso:: :class:`pennylane.tape.QuantumTape`
  
  **Example:**
  
  .. code-block:: python
  
      from pennylane.tape import QuantumScript
  
      ops = [qml.BasisState(np.array([1,1]), wires=(0,"a")),
             qml.RX(0.432, 0),
             qml.RY(0.543, 0),
             qml.CNOT((0,"a")),
             qml.RX(0.133, "a")]
  
      qscript = QuantumScript(ops, [qml.expval(qml.PauliZ(0))])
  
  >>> list(qscript)
  [BasisState(array([1, 1]), wires=[0, "a"]),
  RX(0.432, wires=[0]),
  RY(0.543, wires=[0]),
  CNOT(wires=[0, 'a']),
  RX(0.133, wires=['a']),
  expval(PauliZ(wires=[0]))]
  >>> qscript.operations
  [BasisState(array([1, 1]), wires=[0, "a"]),
  RX(0.432, wires=[0]),
  RY(0.543, wires=[0]),
  CNOT(wires=[0, 'a']),
  RX(0.133, wires=['a'])]
  >>> qscript.measurements
  [expval(PauliZ(wires=[0]))]
  
  Iterating over the quantum script can be done by:
  
  >>> for op in qscript:
  ...     print(op)
  BasisState(array([1, 1]), wires=[0, "a"])
  RX(0.432, wires=[0])
  RY(0.543, wires=[0])
  CNOT(wires=[0, 'a'])
  RX(0.133, wires=['a'])
  expval(PauliZ(wires=[0]))'
  
  Quantum scripts also support indexing and length determination:
  
  >>> qscript[0]
  BasisState(array([1, 1]), wires=[0, "a"])
  >>> len(qscript)
  6
  
  Once constructed, the script can be executed directly on a quantum device
  using the :func:`~.pennylane.execute` function:
  
  >>> dev = qml.device('default.qubit', wires=(0,'a'))
  >>> qml.execute([qscript], dev, gradient_fn=None)
  [array([-0.77750694])]
  
  Quantum scripts can also store information about the number and batches of
  executions by setting the ``shots`` keyword argument. This information is internally
  stored in a :class:`pennylane.measurements.Shots` object:
  
  >>> s_vec = [1, 1, 2, 2, 2]
  >>> qscript = QuantumScript([qml.Hadamard(0)], [qml.expval(qml.PauliZ(0))], shots=s_vec)
  >>> qscript.shots.shot_vector
  (ShotCopies(1 shots x 2), ShotCopies(2 shots x 3))
  
  ``ops`` and ``measurements`` are converted to lists upon initialization,
  so those arguments accept any iterable object:
  
  >>> qscript = QuantumScript((qml.PauliX(i) for i in range(3)))
  >>> qscript.circuit
  [PauliX(wires=[0]), PauliX(wires=[1]), PauliX(wires=[2])]
class_docstr_tok: ["The","operations","and","measurements","that","represent","instructions","for","execution","on","a","quantum","device",".","Args",":","ops","(","Iterable","[","Operator","]",")",":","An","iterable","of","the","operations","to","be","performed","measurements","(","Iterable","[","MeasurementProcess","]",")",":","All","the","measurements","to","be","performed","Keyword","Args",":","shots","(","None",",","int",",","Sequence","[","int","]",",","~.Shots",")",":","Number","and\/or","batches","of","shots","for","execution",".","Note","that","this","property","is","still","experimental","and","under","development",".","trainable_params","(","None",",","Sequence","[","int","]",")",":","the","indices","for","which","parameters","are","trainable","_update=True","(","bool",")",":","Whether","or","not","to","set","various","properties","on","initialization",".","Setting","``","_update=False","``","reduces","computations","if","the","script","is","only","an","intermediary","step",".","..","seealso",":",":",":","class",":","`","pennylane.tape.QuantumTape","`","*","*","Example",":","*","*","..","code-block",":",":","python","from","pennylane.tape","import","QuantumScript","ops","=","[","qml.BasisState","(","np.array","(","[","1,1","]",")",",","wires=","(","0",",","''","a","''",")",")",",","qml.RX","(","0.432",",","0",")",",","qml.RY","(","0.543",",","0",")",",","qml.CNOT","(","(","0",",","''","a","''",")",")",",","qml.RX","(","0.133",",","``","a","''",")","]","qscript","=","QuantumScript","(","ops",",","[","qml.expval","(","qml.PauliZ","(","0",")",")","]",")",">",">",">","list","(","qscript",")","[","BasisState","(","array","(","[","1",",","1","]",")",",","wires=","[","0",",","``","a","''","]",")",",","RX","(","0.432",",","wires=","[","0","]",")",",","RY","(","0.543",",","wires=","[","0","]",")",",","CNOT","(","wires=","[","0",",","'","a","'","]",")",",","RX","(","0.133",",","wires=","[","'","a","'","]",")",",","expval","(","PauliZ","(","wires=","[","0","]",")",")","]",">",">",">","qscript.operations","[","BasisState","(","array","(","[","1",",","1","]",")",",","wires=","[","0",",","``","a","''","]",")",",","RX","(","0.432",",","wires=","[","0","]",")",",","RY","(","0.543",",","wires=","[","0","]",")",",","CNOT","(","wires=","[","0",",","'","a","'","]",")",",","RX","(","0.133",",","wires=","[","'","a","'","]",")","]",">",">",">","qscript.measurements","[","expval","(","PauliZ","(","wires=","[","0","]",")",")","]","Iterating","over","the","quantum","script","can","be","done","by",":",">",">",">","for","op","in","qscript",":","...","print","(","op",")","BasisState","(","array","(","[","1",",","1","]",")",",","wires=","[","0",",","``","a","''","]",")","RX","(","0.432",",","wires=","[","0","]",")","RY","(","0.543",",","wires=","[","0","]",")","CNOT","(","wires=","[","0",",","'","a","'","]",")","RX","(","0.133",",","wires=","[","'","a","'","]",")","expval","(","PauliZ","(","wires=","[","0","]",")",")","'","Quantum","scripts","also","support","indexing","and","length","determination",":",">",">",">","qscript","[","0","]","BasisState","(","array","(","[","1",",","1","]",")",",","wires=","[","0",",","``","a","''","]",")",">",">",">","len","(","qscript",")","6","Once","constructed",",","the","script","can","be","executed","directly","on","a","quantum","device","using","the",":","func",":","`","~.pennylane.execute","`","function",":",">",">",">","dev","=","qml.device","(","'default.qubit","'",",","wires=","(","0",",","'","a","'",")",")",">",">",">","qml.execute","(","[","qscript","]",",","dev",",","gradient_fn=None",")","[","array","(","[","-0.77750694","]",")","]","Quantum","scripts","can","also","store","information","about","the","number","and","batches","of","executions","by","setting","the","``","shots","``","keyword","argument",".","This","information","is","internally","stored","in","a",":","class",":","`","pennylane.measurements.Shots","`","object",":",">",">",">","s_vec","=","[","1",",","1",",","2",",","2",",","2","]",">",">",">","qscript","=","QuantumScript","(","[","qml.Hadamard","(","0",")","]",",","[","qml.expval","(","qml.PauliZ","(","0",")",")","]",",","shots=s_vec",")",">",">",">","qscript.shots.shot_vector","(","ShotCopies","(","1","shots","x","2",")",",","ShotCopies","(","2","shots","x","3",")",")","``","ops","``","and","``","measurements","``","are","converted","to","lists","upon","initialization",",","so","those","arguments","accept","any","iterable","object",":",">",">",">","qscript","=","QuantumScript","(","(","qml.PauliX","(","i",")","for","i","in","range","(","3",")",")",")",">",">",">","qscript.circuit","[","PauliX","(","wires=","[","0","]",")",",","PauliX","(","wires=","[","1","]",")",",","PauliX","(","wires=","[","2","]",")","]"]
docstr: Serialize the circuit as an OpenQASM 2.0 program.
  
  Measurements are assumed to be performed on all qubits in the computational basis. An
  optional ``rotations`` argument can be provided so that output of the OpenQASM circuit is
  diagonal in the eigenbasis of the quantum script's observables. The measurement outputs can be
  restricted to only those specified in the script by setting ``measure_all=False``.
  
  .. note::
  
      The serialized OpenQASM program assumes that gate definitions
      in ``qelib1.inc`` are available.
  
  Args:
      wires (Wires or None): the wires to use when serializing the circuit
      rotations (bool): in addition to serializing user-specified
          operations, also include the gates that diagonalize the
          measured wires such that they are in the eigenbasis of the circuit observables.
      measure_all (bool): whether to perform a computational basis measurement on all qubits
          or just those specified in the script
      precision (int): decimal digits to display for parameters
  
  Returns:
      str: OpenQASM serialization of the circuit
code: def to_openqasm(
      self, wires=None, rotations=True, measure_all=True, precision=None
  ):
      """Serialize the circuit as an OpenQASM 2.0 program.
  
      Measurements are assumed to be performed on all qubits in the computational basis. An
      optional ``rotations`` argument can be provided so that output of the OpenQASM circuit is
      diagonal in the eigenbasis of the quantum script's observables. The measurement outputs can be
      restricted to only those specified in the script by setting ``measure_all=False``.
  
      .. note::
  
          The serialized OpenQASM program assumes that gate definitions
          in ``qelib1.inc`` are available.
  
      Args:
          wires (Wires or None): the wires to use when serializing the circuit
          rotations (bool): in addition to serializing user-specified
              operations, also include the gates that diagonalize the
              measured wires such that they are in the eigenbasis of the circuit observables.
          measure_all (bool): whether to perform a computational basis measurement on all qubits
              or just those specified in the script
          precision (int): decimal digits to display for parameters
  
      Returns:
          str: OpenQASM serialization of the circuit
      """
      wires = wires or self.wires
  
      # add the QASM headers
      qasm_str = 'OPENQASM 2.0;\ninclude "qelib1.inc";\n'
  
      if self.num_wires == 0:
          # empty circuit
          return qasm_str
  
      # create the quantum and classical registers
      qasm_str += f"qreg q[{len(wires)}];\n"
      qasm_str += f"creg c[{len(wires)}];\n"
  
      # get the user applied circuit operations without interface information
      operations = qml.transforms.convert_to_numpy_parameters(
          self
      ).operations
  
      if rotations:
          # if requested, append diagonalizing gates corresponding
          # to circuit observables
          operations += self.diagonalizing_gates
  
      # decompose the queue
      # pylint: disable=no-member
      just_ops = QuantumScript(operations)
      operations = just_ops.expand(
          depth=10, stop_at=lambda obj: obj.name in OPENQASM_GATES
      ).operations
  
      # create the QASM code representing the operations
      for op in operations:
          try:
              gate = OPENQASM_GATES[op.name]
          except KeyError as e:
              raise ValueError(
                  f"Operation {op.name} not supported by the QASM serializer"
              ) from e
  
          wire_labels = ",".join(
              [f"q[{wires.index(w)}]" for w in op.wires.tolist()]
          )
          params = ""
  
          if op.num_params > 0:
              # If the operation takes parameters, construct a string
              # with parameter values.
              if precision is not None:
                  params = (
                      "("
                      + ",".join(
                          [f"{p:.{precision}}" for p in op.parameters]
                      )
                      + ")"
                  )
              else:
                  # use default precision
                  params = (
                      "("
                      + ",".join([str(p) for p in op.parameters])
                      + ")"
                  )
  
          qasm_str += f"{gate}{params} {wire_labels};\n"
  
      # apply computational basis measurements to each quantum register
      # NOTE: This is not strictly necessary, we could inspect self.observables,
      # and then only measure wires which are requested by the user. However,
      # some devices which consume QASM require all registers be measured, so
      # measure all wires by default to be safe.
      if measure_all:
          for wire in range(len(wires)):
              qasm_str += f"measure q[{wire}] -> c[{wire}];\n"
      else:
          measured_wires = Wires.all_wires(
              [m.wires for m in self.measurements]
          )
  
          for w in measured_wires:
              wire_indx = self.wires.index(w)
              qasm_str += f"measure q[{wire_indx}] -> c[{wire_indx}];\n"
  
      return qasm_str
  
imports: import contextlib
  import copy
  from collections import Counter
  from typing import List, Union, Optional, Sequence
  from warnings import warn
  import pennylane
  from pennylane.measurements import ClassicalShadowMP, CountsMP, MeasurementProcess, ProbabilityMP, SampleMP, ShadowExpvalMP, StateMP, Shots
  from pennylane.typing import TensorLike
  from pennylane.operation import Observable, Operator, Operation, _UNSET_BATCH_SIZE
  from pennylane.pytrees import register_pytree
  from pennylane.queuing import AnnotatedQueue, process_queue
  from pennylane.wires import Wires
label_desc: Use image node_id 45 for calling the QuantumScript obj's underlying member method code with example usage: obj.to_openqasm(wires, rotations, measure_all, precision) and returns: qasm_str, qasm_str