ProjectQ-Framework / ProjectQ

ProjectQ: An open source software framework for quantum computing
https://projectq.ch
Apache License 2.0
888 stars 274 forks source link

Cannot find control qubits #313

Closed DrissiReda closed 5 years ago

DrissiReda commented 5 years ago

I cannot find the control qubits inside the cmd object here is a minimal code to reproduce the issue:

from projectq import MainEngine
from projectq.meta import get_control_count
from projectq.ops import X, NOT, C
from pprint import pprint
class Testing(MainEngine):
    def __init__(self):
        MainEngine.__init__(self)
    def receive(self, cmdlist):
        for cmd in cmdlist:
            if cmd.gate == NOT:
                print("control size is {}".format(get_control_count(cmd)))
                print(cmd.control_qubits)
                pprint(vars(cmd))

if __name__ == "__main__":
    eng = Testing()
    q = eng.allocate_qureg(3)
    C(X, n=2) | (q[0], q[1], q[2])

The output is :

control size is 0
[]
{'_control_qubits': [],
 '_engine': <__main__.Testing object at 0x7fec595d67f0>,
 '_qubits': ([<projectq.types._qubit.WeakQubitRef object at 0x7fec42108208>],),
 'gate': <projectq.ops._gates.XGate object at 0x7fec4bf00a58>,
 'tags': []}

Aren't the control qubits supposed to be in the _control_qubits list? The only qubits I can find are the target qubits inside the _qubits list

Takishima commented 5 years ago

The control qubits are only added to the command after it is received by the ControlEngine further down the chain after the MainEngine. For implementation details see “3.2.1 Implementation of meta instructions” in https://doi.org/10.3929/ethz-b-000322770.

To implement a testing engine such as yours, that can be used to check that indeed control qubits are added right after the MainEngine, you could do the following:

test_eng = Testing(BasicEngine) # Or CommandPrinter
eng = projectq.MainEngine(engine_list=[test_eng])
DrissiReda commented 5 years ago

Thank you, that is important information, but I cannot use MainEngine since I want to create a class that inherits from it and adds a few functions, using your method prevents me from getting these functions.

Edit: it actually works even with another class but I would get this output whenever I try to add some more functions, the funny thing is, when I add a change that causes this, even after that change is removed I still get the same output, if I copy that same code (with the removed problematic change) that worked earlier but doesn't work now into another file, the problem would cease to occur :

Traceback (most recent call last):
  File "test.py", line 51, in <module>
    C(X, n=2) | (q[0], q[1], q[2])
  File "/usr/lib64/python3.4/site-packages/projectq/ops/_metagates.py", line 217,in __or__
    self._gate | tuple(gate_quregs)
TypeError: unsupported operand type(s) for |: 'PredefGate' and 'tuple'

Edit2: Also how normal is this?

>>> from projectq import ops
>>> ops.SqrtGate
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SqrtGate'
>>> ops.SqrtSwap
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SqrtSwap'
>>> ops.SqrtX
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SqrtX'
>>> ops.SqrtXGate
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SqrtXGate'
>>> ops.SqrtSwapGate
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SqrtSwapGate'
>>> ops.Swap
<projectq.ops._gates.SwapGate object at 0x7f8d79001fd0>
>>>

The problem only occurs with SqrtSwap and SqrtX the rest of the gates work normally. I'm using Python 3.4.5 and projectq version 0.3.5

Takishima commented 5 years ago

That's perfectly normal, your version of ProjectQ does not have these gates yet.

For the other things, I would suggest opening a new issue and describe in more details what you are trying to achieve. Otherwise it is hard to help.