# Insert measurements to a pre-defined quantumTape

**URL:** <https://discuss.pennylane.ai/t/insert-measurements-to-a-pre-defined-quantumtape/4017>\
**Category:** PennyLane Help\
**Created:** [February 2, 2024, 6:07pm UTC](https://discuss.pennylane.ai/t/insert-measurements-to-a-pre-defined-quantumtape/4017 "2024-02-02T18:07:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkwan314](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/jkwan314/32/1303_2.png) [@jkwan314](https://discuss.pennylane.ai/u/jkwan314)\
**Post date:** [February 2, 2024, 6:07pm UTC](https://discuss.pennylane.ai/t/insert-measurements-to-a-pre-defined-quantumtape/4017/1 "2024-02-02T18:07:04Z")

</div>

Hi,

Is it possible to insert a measurement process in a wrapper function that executes a predefined QuantumTape? For example, instead of

```auto
circuit = qml.tape.QuantumTape(ops, measurements)

def executor(tape):
  qml.execute([tape], dev_noisy)

```

You do something like

```auto
circuit = qml.tape.QuantumTape(ops) # define quantum tape without measurements

def executor(tape):
  # somehow insert measurements here
  qml.execute([tape], dev_noisy)

```

My versions are:

```auto
Name: PennyLane
Version: 0.33.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/bigsad/Downloads/Algorithm-Research/Student-Hub/Indy-Ng/.venv/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning, PennyLane-qiskit

Platform info: macOS-12.6-x86_64-i386-64bit
Python version: 3.11.6
Numpy version: 1.23.5
Scipy version: 1.10.1
Installed devices:
- default.gaussian (PennyLane-0.33.1)
- default.mixed (PennyLane-0.33.1)
- default.qubit (PennyLane-0.33.1)
- default.qubit.autograd (PennyLane-0.33.1)
- default.qubit.jax (PennyLane-0.33.1)
- default.qubit.legacy (PennyLane-0.33.1)
- default.qubit.tf (PennyLane-0.33.1)
- default.qubit.torch (PennyLane-0.33.1)
- default.qutrit (PennyLane-0.33.1)
- null.qubit (PennyLane-0.33.1)
- lightning.qubit (PennyLane-Lightning-0.33.1)
- qiskit.aer (PennyLane-qiskit-0.33.1)
- qiskit.basicaer (PennyLane-qiskit-0.33.1)
- qiskit.ibmq (PennyLane-qiskit-0.33.1)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.33.1)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.33.1)
- qiskit.remote (PennyLane-qiskit-0.33.1)

```

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [February 5, 2024, 7:02pm UTC](https://discuss.pennylane.ai/t/insert-measurements-to-a-pre-defined-quantumtape/4017/2 "2024-02-05T19:02:39Z")

</div>

Hey @jkwan314! One of our PennyLane developers will get back to you shortly about this 🙂

---

<div class="post-metadata">

**Author:** ![timmysilv](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/timmysilv/32/1475_2.png) [@timmysilv](https://discuss.pennylane.ai/u/timmysilv)\
**Post date:** [February 5, 2024, 7:31pm UTC](https://discuss.pennylane.ai/t/insert-measurements-to-a-pre-defined-quantumtape/4017/3 "2024-02-05T19:31:45Z")

</div>

Hi @jkwan314, thanks for stopping by the forum!

Generally speaking, `qml.execute` is a function that takes in a set of tapes and a device, then it returns a set of results. It requires that all information about the circuit being executed is contained on those tape objects, so your best solution is effectively what you originally had (defining the measurements on the tape). If your goal is to modify the measurements within the function, you can simply create a new tape. Here’s a full example of how you might do that:

```python
dev_noisy = qml.device("default.qubit")

ops = [qml.PauliX(0), qml.PauliY(1)]

circuit = qml.tape.QuantumTape(ops)

def executor(tape, new_measurements):
    new_tape = qml.tape.QuantumTape(
        tape.operations,
        tape.measurements + new_measurements,
        shots=tape.shots,
    )
    return qml.execute([new_tape], dev_noisy)

executor(circuit, [qml.state()])

```

Let me know if that works!

---

<div class="post-metadata">

**Author:** ![jkwan314](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/jkwan314/32/1303_2.png) [@jkwan314](https://discuss.pennylane.ai/u/jkwan314)\
**Post date:** [February 6, 2024, 1:51am UTC](https://discuss.pennylane.ai/t/insert-measurements-to-a-pre-defined-quantumtape/4017/4 "2024-02-06T01:51:38Z")

</div>

Thanks, that worked for my usecase!
