test_circuitplot.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from sympy.physics.quantum.circuitplot import labeller, render_label, Mz, CreateOneQubitGate,\
  2. CreateCGate
  3. from sympy.physics.quantum.gate import CNOT, H, SWAP, CGate, S, T
  4. from sympy.external import import_module
  5. from sympy.testing.pytest import skip
  6. mpl = import_module('matplotlib')
  7. def test_render_label():
  8. assert render_label('q0') == r'$\left|q0\right\rangle$'
  9. assert render_label('q0', {'q0': '0'}) == r'$\left|q0\right\rangle=\left|0\right\rangle$'
  10. def test_Mz():
  11. assert str(Mz(0)) == 'Mz(0)'
  12. def test_create1():
  13. Qgate = CreateOneQubitGate('Q')
  14. assert str(Qgate(0)) == 'Q(0)'
  15. def test_createc():
  16. Qgate = CreateCGate('Q')
  17. assert str(Qgate([1],0)) == 'C((1),Q(0))'
  18. def test_labeller():
  19. """Test the labeller utility"""
  20. assert labeller(2) == ['q_1', 'q_0']
  21. assert labeller(3,'j') == ['j_2', 'j_1', 'j_0']
  22. def test_cnot():
  23. """Test a simple cnot circuit. Right now this only makes sure the code doesn't
  24. raise an exception, and some simple properties
  25. """
  26. if not mpl:
  27. skip("matplotlib not installed")
  28. else:
  29. from sympy.physics.quantum.circuitplot import CircuitPlot
  30. c = CircuitPlot(CNOT(1,0),2,labels=labeller(2))
  31. assert c.ngates == 2
  32. assert c.nqubits == 2
  33. assert c.labels == ['q_1', 'q_0']
  34. c = CircuitPlot(CNOT(1,0),2)
  35. assert c.ngates == 2
  36. assert c.nqubits == 2
  37. assert c.labels == []
  38. def test_ex1():
  39. if not mpl:
  40. skip("matplotlib not installed")
  41. else:
  42. from sympy.physics.quantum.circuitplot import CircuitPlot
  43. c = CircuitPlot(CNOT(1,0)*H(1),2,labels=labeller(2))
  44. assert c.ngates == 2
  45. assert c.nqubits == 2
  46. assert c.labels == ['q_1', 'q_0']
  47. def test_ex4():
  48. if not mpl:
  49. skip("matplotlib not installed")
  50. else:
  51. from sympy.physics.quantum.circuitplot import CircuitPlot
  52. c = CircuitPlot(SWAP(0,2)*H(0)* CGate((0,),S(1)) *H(1)*CGate((0,),T(2))\
  53. *CGate((1,),S(2))*H(2),3,labels=labeller(3,'j'))
  54. assert c.ngates == 7
  55. assert c.nqubits == 3
  56. assert c.labels == ['j_2', 'j_1', 'j_0']