Skip to content

Commit

Permalink
Fix program stacking
Browse files Browse the repository at this point in the history
Program stacking have not taken `pq.Q(all)` or `pq.Q()` into account when registering.
This bug has been fixed and additional tests have been written for the problem.
  • Loading branch information
boldar99 committed Aug 5, 2021
1 parent 46e32a8 commit c4058cc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
11 changes: 10 additions & 1 deletion piquasso/api/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ def state(self, new_state):
else None
)

@staticmethod
def _map_modes(register, instruction):
if len(register.modes) == 0:
return instruction.modes
if len(instruction.modes) == 0:
return register.modes

return (register.modes[m] for m in instruction.modes)

def _apply_to_program_on_register(self, program, register):
if self.state is not None:
if program.state is not None:
Expand All @@ -90,7 +99,7 @@ def _apply_to_program_on_register(self, program, register):

instruction_copy._apply_to_program_on_register(
program,
register=Q(*(register.modes[m] for m in instruction.modes))
register=Q(*self._map_modes(register, instruction))
)

def __enter__(self):
Expand Down
56 changes: 56 additions & 0 deletions tests/api/program/test_program_stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,59 @@ def test_state_collision_raises_InvalidProgram(program):
assert error.value.args[0] == (
"The program already has a state registered of type 'FakeState'."
)


def test_all_modes_program_stacking_in_sub_program():
sub_program = pq.Program()
with sub_program:
pq.Q(all) | pq.DummyInstruction(param=10)

with pq.Program() as program:
pq.Q(1, 0, 2) | sub_program
pq.Q(1, 2) | pq.DummyInstruction(param=100)

assert program.instructions[0].modes == (1, 0, 2)
assert program.instructions[0].params == {"param": 10}

assert program.instructions[1].modes == (1, 2)
assert program.instructions[1].params == {"param": 100}


def test_all_modes_program_stacking_in_main_program():
sub_program = pq.Program()
with sub_program:
pq.Q(0, 1) | pq.DummyInstruction(param=10)
pq.Q(2, 3) | pq.DummyInstruction(param=100)

with pq.Program() as program:
pq.Q(all) | sub_program
pq.Q(0, 1) | pq.DummyInstruction(param=1000)

assert program.instructions[0].modes == (0, 1)
assert program.instructions[0].params == {"param": 10}

assert program.instructions[1].modes == (2, 3)
assert program.instructions[1].params == {"param": 100}

assert program.instructions[2].modes == (0, 1)
assert program.instructions[2].params == {"param": 1000}


def test_all_modes_program_stacking_in_both_sub_and_main_program():
sub_program = pq.Program()
with sub_program:
pq.Q(all) | pq.DummyInstruction(param=10)
pq.Q(1, 0) | pq.DummyInstruction(param=100)

with pq.Program() as program:
pq.Q(all) | sub_program
pq.Q(0, 4) | pq.DummyInstruction(param=1000)

assert len(program.instructions[0].modes) == 0
assert program.instructions[0].params == {"param": 10}

assert program.instructions[1].modes == (1, 0)
assert program.instructions[1].params == {"param": 100}

assert program.instructions[2].modes == (0, 4)
assert program.instructions[2].params == {"param": 1000}

0 comments on commit c4058cc

Please sign in to comment.