test_qasm.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from sympy.physics.quantum.qasm import Qasm, flip_index, trim,\
  2. get_index, nonblank, fullsplit, fixcommand, stripquotes, read_qasm
  3. from sympy.physics.quantum.gate import X, Z, H, S, T
  4. from sympy.physics.quantum.gate import CNOT, SWAP, CPHASE, CGate, CGateS
  5. from sympy.physics.quantum.circuitplot import Mz
  6. def test_qasm_readqasm():
  7. qasm_lines = """\
  8. qubit q_0
  9. qubit q_1
  10. h q_0
  11. cnot q_0,q_1
  12. """
  13. q = read_qasm(qasm_lines)
  14. assert q.get_circuit() == CNOT(1,0)*H(1)
  15. def test_qasm_ex1():
  16. q = Qasm('qubit q0', 'qubit q1', 'h q0', 'cnot q0,q1')
  17. assert q.get_circuit() == CNOT(1,0)*H(1)
  18. def test_qasm_ex1_methodcalls():
  19. q = Qasm()
  20. q.qubit('q_0')
  21. q.qubit('q_1')
  22. q.h('q_0')
  23. q.cnot('q_0', 'q_1')
  24. assert q.get_circuit() == CNOT(1,0)*H(1)
  25. def test_qasm_swap():
  26. q = Qasm('qubit q0', 'qubit q1', 'cnot q0,q1', 'cnot q1,q0', 'cnot q0,q1')
  27. assert q.get_circuit() == CNOT(1,0)*CNOT(0,1)*CNOT(1,0)
  28. def test_qasm_ex2():
  29. q = Qasm('qubit q_0', 'qubit q_1', 'qubit q_2', 'h q_1',
  30. 'cnot q_1,q_2', 'cnot q_0,q_1', 'h q_0',
  31. 'measure q_1', 'measure q_0',
  32. 'c-x q_1,q_2', 'c-z q_0,q_2')
  33. assert q.get_circuit() == CGate(2,Z(0))*CGate(1,X(0))*Mz(2)*Mz(1)*H(2)*CNOT(2,1)*CNOT(1,0)*H(1)
  34. def test_qasm_1q():
  35. for symbol, gate in [('x', X), ('z', Z), ('h', H), ('s', S), ('t', T), ('measure', Mz)]:
  36. q = Qasm('qubit q_0', '%s q_0' % symbol)
  37. assert q.get_circuit() == gate(0)
  38. def test_qasm_2q():
  39. for symbol, gate in [('cnot', CNOT), ('swap', SWAP), ('cphase', CPHASE)]:
  40. q = Qasm('qubit q_0', 'qubit q_1', '%s q_0,q_1' % symbol)
  41. assert q.get_circuit() == gate(1,0)
  42. def test_qasm_3q():
  43. q = Qasm('qubit q0', 'qubit q1', 'qubit q2', 'toffoli q2,q1,q0')
  44. assert q.get_circuit() == CGateS((0,1),X(2))
  45. def test_qasm_flip_index():
  46. assert flip_index(0, 2) == 1
  47. assert flip_index(1, 2) == 0
  48. def test_qasm_trim():
  49. assert trim('nothing happens here') == 'nothing happens here'
  50. assert trim("Something #happens here") == "Something "
  51. def test_qasm_get_index():
  52. assert get_index('q0', ['q0', 'q1']) == 1
  53. assert get_index('q1', ['q0', 'q1']) == 0
  54. def test_qasm_nonblank():
  55. assert list(nonblank('abcd')) == list('abcd')
  56. assert list(nonblank('abc ')) == list('abc')
  57. def test_qasm_fullsplit():
  58. assert fullsplit('g q0,q1,q2, q3') == ('g', ['q0', 'q1', 'q2', 'q3'])
  59. def test_qasm_fixcommand():
  60. assert fixcommand('foo') == 'foo'
  61. assert fixcommand('def') == 'qdef'
  62. def test_qasm_stripquotes():
  63. assert stripquotes("'S'") == 'S'
  64. assert stripquotes('"S"') == 'S'
  65. assert stripquotes('S') == 'S'
  66. def test_qasm_qdef():
  67. # weaker test condition (str) since we don't have access to the actual class
  68. q = Qasm("def Q,0,Q",'qubit q0','Q q0')
  69. assert str(q.get_circuit()) == 'Q(0)'
  70. q = Qasm("def CQ,1,Q", 'qubit q0', 'qubit q1', 'CQ q0,q1')
  71. assert str(q.get_circuit()) == 'C((1),Q(0))'