Skip to content

Commit

Permalink
Merge a74af4e into a41a96c
Browse files Browse the repository at this point in the history
  • Loading branch information
QFer authored Apr 12, 2019
2 parents a41a96c + a74af4e commit 4761755
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
28 changes: 12 additions & 16 deletions src/quantuminspire/projectq/backend_qx.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def is_available(self, cmd: Command) -> bool:
return True
if count != 0:
return False
if g in (T, Tdag, S, Sdag, H, X, Y, Z):
if g in (T, Tdag, S, Sdag, Swap, H, X, Y, Z):
return True
elif isinstance(g, (Rx, Ry, Rz)):
return True
Expand Down Expand Up @@ -162,7 +162,7 @@ def _store(self, cmd: Command) -> None:
# this case also covers the CX controlled gate
ctrl_pos = cmd.control_qubits[0].id
qb_pos = cmd.qubits[0][0].id
self.qasm += "\nCNOT q[{}], q[{}]".format(ctrl_pos, qb_pos)
self.qasm += "\ncnot q[{}], q[{}]".format(ctrl_pos, qb_pos)
elif gate == Swap:
q0 = cmd.qubits[0][0].id
q1 = cmd.qubits[1][0].id
Expand All @@ -171,11 +171,11 @@ def _store(self, cmd: Command) -> None:
ctrl_pos1 = cmd.control_qubits[0].id
ctrl_pos2 = cmd.control_qubits[1].id
qb_pos = cmd.qubits[0][0].id
self.qasm += "\nToffoli q[{}], q[{}], q[{}]".format(ctrl_pos1, ctrl_pos2, qb_pos)
self.qasm += "\ntoffoli q[{}], q[{}], q[{}]".format(ctrl_pos1, ctrl_pos2, qb_pos)
elif gate == Z and get_control_count(cmd) == 1:
ctrl_pos = cmd.control_qubits[0].id
qb_pos = cmd.qubits[0][0].id
self.qasm += "\nCZ q[{}], q[{}]".format(ctrl_pos, qb_pos)
self.qasm += "\ncz q[{}], q[{}]".format(ctrl_pos, qb_pos)
elif gate == Barrier:
qb_pos = [qb.id for qr in cmd.qubits for qb in qr]
self.qasm += "\n# barrier gate "
Expand All @@ -186,25 +186,24 @@ def _store(self, cmd: Command) -> None:
elif isinstance(gate, Rz) and get_control_count(cmd) == 1:
ctrl_pos = cmd.control_qubits[0].id
qb_pos = cmd.qubits[0][0].id
gate_name = 'CR'
gate_name = 'cr'
self.qasm += "\n{} q[{}],q[{}],{:.12f}".format(gate_name, ctrl_pos, qb_pos, gate.angle)
elif isinstance(gate, (Rx, Ry)) and get_control_count(cmd) == 1:
raise NotImplementedError('controlled Rx or Ry gate not implemented')
elif isinstance(gate, (Rx, Ry, Rz)):
assert get_control_count(cmd) == 0
qb_pos = cmd.qubits[0][0].id
gate_name = str(gate)[0:2]
gate_name = str(gate)[0:2].lower()
self.qasm += "\n{} q[{}],{:.12g}".format(gate_name, qb_pos, gate.angle)
elif gate == Tdag and get_control_count(cmd) == 0:
qb_pos = cmd.qubits[0][0].id
self.qasm += "\nTdag q[{}]".format(qb_pos)
elif isinstance(gate, tuple(type(gate) for gate in (X, Y, Z, H, S, Sdag, T, Tdag))):
self.qasm += "\ntdag q[{}]".format(qb_pos)
elif gate == Sdag and get_control_count(cmd) == 0:
qb_pos = cmd.qubits[0][0].id
self.qasm += "\nsdag q[{}]".format(qb_pos)
elif isinstance(gate, tuple(type(gate) for gate in (X, Y, Z, H, S, T))):
assert get_control_count(cmd) == 0
if str(gate) in self._gate_names:
gate_str = self._gate_names[str(gate)]
else:
gate_str = str(gate).lower()

gate_str = str(gate).lower()
qb_pos = cmd.qubits[0][0].id
self.qasm += "\n{} q[{}]".format(gate_str, qb_pos)
else:
Expand Down Expand Up @@ -429,6 +428,3 @@ def __add_measure_all_qubits(self) -> None:
for _ in range(qubits_counts):
q = qubits_reference.pop()
Measure | q

""" Mapping of gate names from our gate objects to the cQASM representation. """
_gate_names = {str(Tdag): "Tdag", str(Sdag): "Sdag"}
23 changes: 11 additions & 12 deletions src/tests/quantuminspire/projectq/test_backend_qx.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_is_available_correct_result(self):
self.__is_available_assert_equal(Ph(0.4), False)
self.__is_available_assert_equal(Toffoli, False)
for gate in [Measure, Allocate, Deallocate, Barrier, T, Tdag,
S, Sdag, H, X, Y, Z, Rx(0.1), Ry(0.2), Rz(0.3)]:
S, Sdag, Swap, H, X, Y, Z, Rx(0.1), Ry(0.2), Rz(0.3)]:
self.__is_available_assert_equal(gate, True)

def test_reset_is_cleared(self):
Expand Down Expand Up @@ -124,25 +124,24 @@ def __store_function_raises_error(self, gate, function_mock, count=0):
def test_store_returns_correct_qasm(self):
angle = 0.1
self.__store_function_assert_equal(0, NOT, "\nx q[0]")
self.__store_function_assert_equal(1, NOT, "\nCNOT q[0], q[1]", count=1)
self.__store_function_assert_equal(1, NOT, "\ncnot q[0], q[1]", count=1)
self.__store_function_assert_equal(0, Swap, "\nswap q[0], q[1]")
self.__store_function_assert_equal(1, X, "\nToffoli q[0], q[1], q[1]", count=2)
self.__store_function_assert_equal(1, Z, "\nCZ q[0], q[1]", count=1)
self.__store_function_assert_equal(1, X, "\ntoffoli q[0], q[1], q[1]", count=2)
self.__store_function_assert_equal(1, Z, "\ncz q[0], q[1]", count=1)
self.__store_function_assert_equal(0, Barrier, "\n# barrier gate q[0], q[1];")
self.__store_function_assert_equal(1, Rz(angle), "\nCR q[0],q[1],{0:.12f}".format(angle), count=1)
self.__store_function_assert_equal(1, Rz(angle), "\nCR q[0],q[1],{0:.12f}".format(angle), count=1)
self.__store_function_assert_equal(1, Rx(angle), "\nRx q[1],{0}".format(angle))
self.__store_function_assert_equal(1, Ry(angle), "\nRy q[1],{0}".format(angle))
self.__store_function_assert_equal(1, Rz(angle), "\nRz q[1],{0}".format(angle))
self.__store_function_assert_equal(0, Tdag, "\nTdag q[0]")
self.__store_function_assert_equal(1, Rz(angle), "\ncr q[0],q[1],{0:.12f}".format(angle), count=1)
self.__store_function_assert_equal(1, Rz(angle), "\ncr q[0],q[1],{0:.12f}".format(angle), count=1)
self.__store_function_assert_equal(1, Rx(angle), "\nrx q[1],{0}".format(angle))
self.__store_function_assert_equal(1, Ry(angle), "\nry q[1],{0}".format(angle))
self.__store_function_assert_equal(1, Rz(angle), "\nrz q[1],{0}".format(angle))
self.__store_function_assert_equal(0, X, "\nx q[0]")
self.__store_function_assert_equal(0, Y, "\ny q[0]")
self.__store_function_assert_equal(0, Z, "\nz q[0]")
self.__store_function_assert_equal(0, H, "\nh q[0]")
self.__store_function_assert_equal(0, S, "\ns q[0]")
self.__store_function_assert_equal(0, Sdag, "\nSdag q[0]")
self.__store_function_assert_equal(0, Sdag, "\nsdag q[0]")
self.__store_function_assert_equal(0, T, "\nt q[0]")
self.__store_function_assert_equal(0, Tdag, "\nTdag q[0]")
self.__store_function_assert_equal(0, Tdag, "\ntdag q[0]")

def test_store_raises_error(self):
angle = 0.1
Expand Down

0 comments on commit 4761755

Please sign in to comment.