I want to remove the ancilla qubits from my quantum circuit in Qiskit. My MWE below contains four qubits: psi0, psi1, anc0, anc1. I want to keep psi0 and psi1 qubits and remove ancillas anc0, anc1. Is there a way to do this?
from qiskit import QuantumRegister, QuantumCircuit, AncillaRegister, transpile
from qiskit_aer import StatevectorSimulator
import numpy as np
reg1 = QuantumRegister(2, 'psi')
reg2 = AncillaRegister(2, 'anc')
circ = QuantumCircuit(reg1, reg2)
circ.h(reg1[0])
circ.h(reg1[1])
circ.prepare_state(1/np.sqrt(2)np.array([1,0,0,1]),reg2)
circ.cry(np.pi/2.2, control_qubit=reg2[1], target_qubit=reg1[1])
circ.cry(np.pi/1.5, control_qubit=reg2[0], target_qubit=reg1[0])
circ.prepare_state(1/np.sqrt(2)np.array([1,0,0,1]),reg2).inverse()
simulator = StatevectorSimulator()
job = transpile(circ, simulator) # execute circuit on qasm simulator
result = simulator.run(job,shots=128).result() # results from the job
out_state = result.get_statevector()
print(out_state)
circ.draw('mpl')
I'm aware of a function that removes idle wires, but it loses the register's info. Is there a different function that specifically removes ancilla qubits (and keeps the register's info)?
Edit: Provided clearer circuit example. To clarify: Yes it will be ok to keep the ancilla qubits until the end of the circuit. I just need psi qubits in the next part of the calculation. I want to do a statevector simulation of psi, not anc, at the end. Ideally I don't want to initialize a separate circuit using the output density matrix -- I prefer to use one self-contained circuit.
Edit 2: The comments below suggested simulating the full statevector of all qubits, then taking the trace to remove ancillas. But the partial_trace function can only be applied to density matrices (see this post).
What is an equivalent method to trace over statevectors?
Is it possible to automatically determine all the ancillas and trace them out?
If there are many ancillas, will it be computationally expensive to simulate the full statevector rather than removing the ancillas before simulation?
