Hello Pennylane Community,
I am wondering what would be the most efficient way to create a superposition of two states in Pennylane? Say I have two quantum circuits circuit1 and circuit2 and they generate some states |\phi \rangle and |\psi \rangle. Is there a way in Pennylane to directly add them together and renormalize the state such that you get a new state like |\Phi \rangle = \alpha |\phi \rangle +\beta |\psi \rangle with arbitrary but normalization preserving \alpha and \beta? Of course I can also write a quantum circuit with post-selection that does it, but maybe there is (or should be) a way to do this directly in Pennylane so I dont need the memory for an additional ancilla.
I only found qml.Superposition to create superpositions of computational basis states but this is too restricted for me.
Thanks for any input 
Hi @RisingPhoelix , welcome to the Forum!
I guess you could use qml.StatePrep. You would need to return the state of each subcircuit, do some processing, and then plug it into a new circuit using StatePrep. Note that StatePrep has a normalization option, so this part of the question would be solved.
Is this what you were looking for?
Hi @CatalinaAlbornoz, thanks for the suggestion! Does this work with the MPS device or would the state measurement create the full 2^N-dimensional state vector instead of simply adding the two MPS together? Anyways, I’ll give it a try and let you know, thank you!
Update: No, it doesnt work since qml.state() indeed returns the full state vector instead of the compressed MPS. Is there some way to extract an MPS, modify them and preparing it in a quantum circuit with Pennylane, @CatalinaAlbarnoz?
Hi @RisingPhoelix , thanks for reporting this.
Let me check and get back to you on this.
Hi @RisingPhoelix ,
I’m having trouble finding an answer. Could you give me some more context for your question or some pseudocode of what you would like to do?
My understanding is that you want some compressed representation of the state from two separate circuits and then preparing a third circuit with a superposition of both states, but again in some compressed way. Is that right?
Hi @CatalinaAlbornoz ,
Exactly! Here’s an example what I want to do:
import numpy as np
import pennylane as qml
qubits = 18 # works with 18 qubits, but not with 50 qubits as I would expect with MPS
dev = qml.device("default.tensor", wires=qubits, method="mps", max_bond_dim=10, contract="auto-mps")
@qml.qnode(dev)
def circuit_a(x1):
for i in range(qubits):
qml.RX(x1[i], wires=i)
qml.CNOT(wires=[i, (i + 1) % 4])
return qml.state()
@qml.qnode(dev)
def circuit_b(x2):
for i in range(qubits):
qml.RY(x2[i], wires=i)
qml.CNOT(wires=[i, (i + 1) % 4])
qml.X(wires=0)
return qml.state()
x1 = np.array([0.1] * qubits)
x2 = np.array([0.2] * qubits)
psi = circuit_a(x1)
phi = circuit_b(x2)
# I want to add the MPS representations, not the actual 2^qubits-dimensional states.
# But psi and phi are not MPS, they are just states.
superposition = (psi + phi) / np.linalg.norm(psi + phi)
@qml.qnode(dev)
def superposition_circuit(state):
qml.StatePrep(state, wires=range(qubits))
for i in range(qubits):
qml.RX(0.1, wires=i)
return [qml.expval(qml.PauliZ(i)) for i in range(qubits)]
result = np.array(superposition_circuit(superposition))
print(result)
Hope this makes things clear. I know there is an alternative way by using an ancilla qubit with a post-selection measurement, but I would like to reduce the overhead as much as possible since MPS addition would be very simple and efficient.
Hi @RisingPhoelix ,
I think our demo on Constant-depth preparation of matrix product states contains the solution to your question.
It has a sequential_preparation subroutine to prepare the Matrix Product States and then a section on Fusion of MPS states, and also one on operator pushing to correct some defect matrices. This looks a lot like what you’re describing.
I recommend that you take a close look at the full demo, try out the code and let me know if this works for what you need!