Skip to content

Commit

Permalink
Add 'controls' and 'tags' parameters to Command.__init__ (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc authored and thomashaener committed Apr 22, 2017
1 parent d8b1d42 commit 2ae3608
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
3 changes: 1 addition & 2 deletions projectq/backends/_ibm/_ibm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def test_ibm_backend_is_available_control_not(num_ctrl_qubits, is_available):
qubit1 = eng.allocate_qubit()
qureg = eng.allocate_qureg(num_ctrl_qubits)
ibm_backend = _ibm.IBMBackend()
cmd = Command(eng, NOT, (qubit1,))
cmd.add_control_qubits(qureg)
cmd = Command(eng, NOT, (qubit1,), controls=qureg)
assert ibm_backend.is_available(cmd) == is_available


Expand Down
56 changes: 32 additions & 24 deletions projectq/ops/_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Command(object):
all_qubits: A tuple of control_qubits + qubits
"""

def __init__(self, engine, gate, qubits):
def __init__(self, engine, gate, qubits, controls=(), tags=()):
"""
Initialize a Command object.
Expand All @@ -93,18 +93,25 @@ def __init__(self, engine, gate, qubits):
(see WeakQubitRef).
Args:
engine: engine which created the qubit (mostly the MainEngine)
gate: Gate to be executed
qubits: Tuple of quantum registers (to which the gate is applied)
engine (projectq.cengines.BasicEngine):
engine which created the qubit (mostly the MainEngine)
gate (projectq.ops.Gate):
Gate to be executed
qubits (tuple[Qureg]):
Tuple of quantum registers (to which the gate is applied)
controls (Qureg|list[Qubit]):
Qubits that condition the command.
tags (list[object]):
Tags associated with the command.
"""
qubits = tuple([[WeakQubitRef(qubit.engine, qubit.id)
for qubit in qreg]
for qreg in qubits])
qubits = tuple([WeakQubitRef(qubit.engine, qubit.id)
for qubit in qreg]
for qreg in qubits)

self.gate = gate
self.tags = []
self.tags = list(tags)
self.qubits = qubits # property
self._control_qubits = [] # access via self.control_qubits property
self.control_qubits = controls # property
self.engine = engine # property

@property
Expand All @@ -117,10 +124,11 @@ def qubits(self, qubits):

def __deepcopy__(self, memo):
""" Deepcopy implementation. Engine should stay a reference."""
cpy = Command(self.engine, deepcopy(self.gate), self.qubits)
cpy.tags = deepcopy(self.tags)
cpy.add_control_qubits(self.control_qubits)
return cpy
return Command(self.engine,
deepcopy(self.gate),
self.qubits,
list(self.control_qubits),
deepcopy(self.tags))

def get_inverse(self):
"""
Expand All @@ -133,11 +141,11 @@ def get_inverse(self):
NotInvertible: If the gate does not provide an inverse (see
BasicGate.get_inverse)
"""
cmd = Command(self._engine, projectq.ops.get_inverse(self.gate),
self.qubits)
cmd.tags = deepcopy(self.tags)
cmd.add_control_qubits(self.control_qubits)
return cmd
return Command(self._engine,
projectq.ops.get_inverse(self.gate),
self.qubits,
list(self.control_qubits),
deepcopy(self.tags))

def get_merged(self, other):
"""
Expand All @@ -152,12 +160,12 @@ def get_merged(self, other):
or can't be merged for other reasons.
"""
if (self.tags == other.tags and self.all_qubits == other.all_qubits and
self.engine == other.engine):
merged_command = Command(self.engine, self.gate, self.qubits)
merged_command.gate = merged_command.gate.get_merged(other.gate)
merged_command.add_control_qubits(self.control_qubits)
merged_command.tags = deepcopy(self.tags)
return merged_command
self.engine == other.engine):
return Command(self.engine,
self.gate.get_merged(other.gate),
self.qubits,
self.control_qubits,
deepcopy(self.tags))
raise projectq.ops.NotMergeable("Commands not mergeable.")

def _order_qubits(self, qubits):
Expand Down
4 changes: 2 additions & 2 deletions projectq/ops/_metagates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ def test_controlled_gate_or():
qubit1 = Qubit(main_engine, 1)
qubit2 = Qubit(main_engine, 2)
qubit3 = Qubit(main_engine, 3)
expected_cmd = Command(main_engine, gate, ([qubit3],))
expected_cmd.add_control_qubits([qubit0, qubit1, qubit2])
expected_cmd = Command(main_engine, gate, ([qubit3],),
controls=[qubit0, qubit1, qubit2])
received_commands = []
# Option 1:
_metagates.ControlledGate(gate, 3) | ([qubit1], [qubit0],
Expand Down

0 comments on commit 2ae3608

Please sign in to comment.