1

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

1 Answers1

1

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!

isaac
  • 171
  • 1