3
$\begingroup$

Suppose one of my functions create some ancillary qubits and reset them. Then, another function wants to create a larger number of ancillary qubits. So, I'd like to reuse the first set of qubits and add the difference two them. Is there any efficient way to achieve the goal?

def function1(qc):
    r = 3
    ancillas = QuantumRegister(r, 'ancillas')
    qc.add(sum_q)
    # use the ancillas
    return ancillas

def function2(qc, partial_ancillas):
    r = 5
    diff = r - len(partial_ancillas)
    if diff > 0:
        # something like ancillas = partial_ancillas.add(diff)
        # or maybe ancillas = partial_ancillas + QuantumRegister(diff)
    else:
        ancillas = partial_ancillas
    return ancillas

qc = QuantumCircuit()
anc = function1(qc)
anc = function2(qc, anc)
$\endgroup$
  • 1
    $\begingroup$ There are ways to do this, as the answer explains, but they aren't so elegant. Since people, like you, want to be able to do this in a clean way, this functionality is in development and will be released soon.' $\endgroup$ – James Wootton Dec 23 '18 at 10:10
1
$\begingroup$

I don't think you can add qubits in a QuantumRegister already set. If your goal however is to create another ancilla registers that cannot exceed a limit of qubits, what you can do is :

def create_ancillas(r=3):

    ancillas = QuantumRegister(r, 'ancillas')
    return ancillas

def create_ancillas_limited(partial_ancillas,r=5):

    diff = r - partial_ancillas.size
    if diff > 0:
        return QuantumRegister(diff, 'ancillas_2')
    else:
        return None

qc = QuantumCircuit()
anc1 = create_ancillas()
qc.add(anc1)

anc2 = create_ancillas_limited(anc1)
qc.add(anc2)

Maybe better if you want to limit the number of qubits in your circuits when adding ancillas is :

def add_ancillas_limited(qc,n_ancillas_to_add=3,limit=5):
    if qc.width() - n_ancillas_to_add > limit:
        print("Cannot exceed limit!")
    else:
        qc.add(QuantumRegister(n_ancillas_to_add))
$\endgroup$
  • $\begingroup$ More or less, this is the workaround I've been using so far. However, I was hoping for a cleaner solution. $\endgroup$ – tigerjack89 Dec 21 '18 at 15:54
  • $\begingroup$ @tigerjack89 What is cleaner for you ? $\endgroup$ – cnada Dec 21 '18 at 15:58
  • $\begingroup$ A built-in function or an officially proposed pattern. $\endgroup$ – tigerjack89 Dec 21 '18 at 16:42
  • $\begingroup$ I was on a rush before, but the problem with your (and mine approach) is that I can't use it for general purpose functions without complicating the code too much. For example, consider the case in which I'd like to implement a multi-control CNOT function. If I have a single QuantumRegister it's quite easy. However, if I have a bunch of them, things get messy. I was hoping for a way to create a (pseudo) QuantumRegister containing other registers inside it, but maybe the best solution is to it myself. What do you think? $\endgroup$ – tigerjack89 Dec 21 '18 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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