Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConjugationEnvironment error #46

Open
diehoq opened this issue Apr 26, 2024 · 5 comments
Open

ConjugationEnvironment error #46

diehoq opened this issue Apr 26, 2024 · 5 comments

Comments

@diehoq
Copy link
Contributor

diehoq commented Apr 26, 2024

The following code raises Exception: Tried to perform operation cswap on unallocated qubit qbl_392.0, which seems to be a bug

def to_be_debugged(x, p, m):
    n = p.bit_length()
    u = qrisp.QuantumFloat(n)
    u[:] = p
    v = qrisp.QuantumFloat(n)
    v[:] = x
    r = qrisp.QuantumModulus(2 * p)
    r[:] = 0
    s = qrisp.QuantumModulus(2 * p)
    s[:] = 1

    f = qrisp.QuantumBool()
    f[:] = True
    for i in range(2 * n):
        a = qrisp.QuantumBool()

        with qrisp.control(a):
            qrisp.swap(u, v)
            qrisp.swap(r, s)
        # uncompute a
        qrisp.mcx([s[0]], a, ctrl_state="0")
        a.delete()

    r = p - r
    res = qrisp.QuantumModulus(p)
    for i in range(res.size):
        qrisp.swap(res[i], r[i])

    # Uncompute u,v,s,f
    f.flip()
    f.delete()
    qrisp.x(u)
    u.delete()
    v.delete()
    r.delete()
    s -= p
    s.delete()
    return res
p=7
t1 = qrisp.QuantumModulus(p)
t1[:] = 6
t2 = qrisp.QuantumModulus(p)
t2[:] = 2
m = qrisp.QuantumArray(qtype=qrisp.QuantumBool(), shape=(2*p.bit_length(),))
with qrisp.conjugate(to_be_debugged)(t1,p,m) as q:
    s = q*t2
for a in m:
    a.delete(verify=True)

ERROR:

{--------------------------------------------------------------------------- Exception Traceback (most recent call last) Cell In[29], [line 7](vscode-notebook-cell:?execution_count=29&line=7) [5](vscode-notebook-cell:?execution_count=29&line=5) t2[:] = 2 [6](vscode-notebook-cell:?execution_count=29&line=6) m = qrisp.QuantumArray(qtype=qrisp.QuantumBool(), shape=(2*p.bit_length(),)) ----> [7](vscode-notebook-cell:?execution_count=29&line=7) with qrisp.conjugate(to_be_debugged)(t1,p,m) as q: [8](vscode-notebook-cell:?execution_count=29&line=8) s = q*t2 [9](vscode-notebook-cell:?execution_count=29&line=9) for a in m:

File ~\Documents\GitHub\Qrisp\src\qrisp\environments\conjugation_environment.py:163, in ConjugationEnvironment.enter(self)
161 instr = temp_data.pop(i)
162 if isinstance(instr, QuantumEnvironment):
--> 163 instr.compile()
164 else:
165 self.env_qs.data.append(instr)

File ~\Documents\GitHub\Qrisp\src\qrisp\environments\control_environment.py:336, in ControlEnvironment.compile(self)
332 instruction.qubits = [self.condition_truth_value] + list(
333 instruction.qubits
334 )
335 # Append instruction
--> 336 self.env_qs.append(instruction)
338 perm_unlock(ctrl_qubits)
340 if inversion_tracker == -1:

File ~\Documents\GitHub\Qrisp\src\qrisp\core\quantum_session.py:446, in QuantumSession.append(self, operation_or_instruction, qubits, clbits)
444 if issubclass(operation_or_instruction.class, Instruction):
445 instruction = operation_or_instruction
--> 446 self.append(instruction.op, instruction.qubits, instruction.clbits)
447 return
450 elif issubclass(operation_or_instruction.class, Operation):

File ~\Documents\GitHub\Qrisp\src\qrisp\core\quantum_session.py:496, in QuantumSession.append(self, operation_or_instruction, qubits, clbits)
490 raise Exception(
491 f"Tried to perform operation {operation.name} on "
492 f"unallocated qubit {input}"
493 )
495 if operation.name not in ["qb_alloc", "barrier"]:
--> 496 check_alloc(qubits)
499 # We now need to merge the sessions and treat their differing environment
500 # levels. The idea here is that if a quantum session A is not identical to the
501 # environment session B, there have been no gates applied within that
502 # environment so far (otherwise merging would have occured). Thus, all data of A
503 # belongs into the original_data attribute of the environment with the highest
504 # level environment, where the environment quantum session isn't identical to A.
505 flattened_qubits = []

File ~\Documents\GitHub\Qrisp\src\qrisp\core\quantum_session.py:486, in QuantumSession.append..check_alloc(input, res)
484 if isinstance(input, list):
485 for item in input:
--> 486 check_alloc(item)
487 else:
488 if not input.allocated:
489 # pass

File ~\Documents\GitHub\Qrisp\src\qrisp\core\quantum_session.py:490, in QuantumSession.append..check_alloc(input, res)
487 else:
488 if not input.allocated:
489 # pass
--> 490 raise Exception(
491 f"Tried to perform operation {operation.name} on "
492 f"unallocated qubit {input}"
493 )

Exception: Tried to perform operation cswap on unallocated qubit qbl_400.0
}

@positr0nium
Copy link
Contributor

positr0nium commented Apr 28, 2024

Hi Diego,
I fixed the issue within 46bdb97 . Your code unfortunately has more problems. The QuantumVariables res and r are allocated within the function and are not being uncomputed. This should however be managable by allocating res and r prior to the conjugation and replacing the line r = p - r with an in-place function.

@positr0nium
Copy link
Contributor

The following in-place function should do :)

from qrisp import *

# Performs a modular inplace right subtraction, ie.
# r = p - r
def inpl_rsub(r, p):
    x(r)
    r.inpl_adder(r.modulus+1, r)
    r += p

# Test function for a variety of inputs
for n in [5, 7, 13, 17, 19]:
    for p in range(n):
        
        r = QuantumModulus(n)
        
        h(r)
        init_r = r.duplicate(init = True)
        
        inpl_rsub(r, p)
        
        mes_res = multi_measurement([init_r, r])
        
        for init_r, r in mes_res.keys():
            if init_r is np.nan:
                continue
            if (p - init_r)%n != r:
                print(f"Adder failed for input constellation r = {init_r}, p = {p}")
                raise

@diehoq
Copy link
Contributor Author

diehoq commented Apr 29, 2024

Thank you for the fix. I get what you mean for the QuantumVariable res, but r is indeed uncomputed within the algorithm. Do you mean something else?

@positr0nium
Copy link
Contributor

With the line
r = r - p
You create a new QuantumVariable and call it r. The reference to the previous r is lost. The new r is uncomputed, while the old r stays alive. You can fix it by using the in-place function that I gave, because it doesn't create a new QuantumVariable

@diehoq
Copy link
Contributor Author

diehoq commented Apr 29, 2024

Ah yes, indeed, and in this case you won't need to allocate it prior the conjugation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants