1
$\begingroup$

I want to use a pennylane quantum function that i created earlier somewhere else, again in a new function but have it applied to certain wires. The reason for this is, that I have a quantum circuit of which a sub-part i want to test out with different circuits. So instead of writing the whole circuit from start everytime for each sub-circuit variant, i would prefere to define all my sub-circuit functions somewhere before and then insert them in my main Circuit when i want to try out a specific sub-circuit. In essence i am looking for a function similar to the qiskit functions circuit_to_gate (https://qiskit.org/documentation/stubs/qiskit.converters.circuit_to_gate.html)

Thanks a lot

$\endgroup$

1 Answer 1

1
$\begingroup$

One way you can do this is to use qml.map_wires (NOTE: you need pennylane>=0.27 for this). Here is an example:

Let's say this is the quantum function that you want to use in QNodes later on:

def quantum_function():
    qml.PauliX(0)
    qml.PauliY(1)
    qml.PauliZ(2)

Let's create a circuit with 8 wires and see what this looks like:

dev = qml.device("default.qubit", wires=8)
@qml.qnode(dev)
def circuit():
    quantum_function()
    return [qml.expval(qml.PauliZ(w)) for w in dev.wires]
>>> print(qml.draw(circuit)())
0: ──X─┤  <Z>
1: ──Y─┤  <Z>
2: ──Z─┤  <Z>
3: ────┤  <Z>
4: ────┤  <Z>
5: ────┤  <Z>
6: ────┤  <Z>
7: ────┤  <Z>

Now, let's create a mapped version of quantum_function with the qml.map_wires() transformation:

wire_map = {0: 4, 1: 2, 2: 7} # this defines how we want to map the wires
mapped_quantum_function = qml.map_wires(quantum_function, wire_map)

Now,

@qml.qnode(dev)
def circuit_with_mapping():
    mapped_quantum_function()
    return [qml.expval(qml.PauliZ(w)) for w in dev.wires]
>>> print(qml.draw(circuit_with_mapping)())
0: ────┤  <Z>
1: ────┤  <Z>
2: ──Y─┤  <Z>
3: ────┤  <Z>
4: ──X─┤  <Z>
5: ────┤  <Z>
6: ────┤  <Z>
7: ──Z─┤  <Z>

Hope this helps!

$\endgroup$
3
  • $\begingroup$ Thank you Isaac, that is exactly what i was looking for. $\endgroup$ Dec 7, 2022 at 11:29
  • $\begingroup$ Awesome! Glad this worked for you 🎉 $\endgroup$
    – isaac
    Dec 8, 2022 at 15:08
  • $\begingroup$ I believe another option can be to give the subroutine you write a keyword argument wires with which you control on which qubits the gates in the quantum function are applied :) $\endgroup$ Feb 11 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.