为什么我的代码不能用于循环和 if 条件?

Why my code in not working for loops and if conditions?

我正尝试在 qutip 中 运行 这段代码。但它没有显示任何结果。但是没有显示任何错误。所有代码都是正确的,没有显示任何错误。但是不知道为什么程序不执行。如果有人能解决这个问题,欢迎。

  from __future__ import division
  from qutip import *
  import pylab
  import math
  import matplotlib.pyplot as plt
  import numpy as np
  import csv
  #import plotly.plotly as py
  #from plotly.graph_objs import *
  #py.sign_in("cayayald", "zlae2s4d1i")
  #linewidth
  #gamma = 1 => fully independent
  #G = 1 => fully collective
  gamma = .25
  G = 1.0 - gamma
  #delta is detuning
  delta = -1
  deltastep = 0.1
  dlist = []
  #n is number of atoms
  n = 16
  #V is energy shiftdue to dipole-dipole
  V = 10
  #omega is rabi freq
  omega = 1.5
  omegastep = 0
  #lowering, raising, and state collapsing operators
  sigmamlist = []
  sigmaplist = []
  c_op_list =  []
  for j in range (n):
   if j==0:
   sigmajm=sigmam()
   else:
   sigmajm=qeye(2)
   for i in range (1,n):
   if j == i:
   sigmaj1= tensor(sigmam(),sigmajm)
   else:
   sigmajm = tensor(qeye(2),sigmajm)
   sigmamlist.append(sigmajm)


  coplist.append(math.sqrt(gamma)*sigmajm) #n-many indep collapse ops
  sigmajp = sigmajm.dag()
  sigmaplist.append(sigmajp)
  coplist.append(math.sqrt(G)*sum(sigmamlist)) #one collective collapse op
  #we want expectation values of operators in exoplist
  exoplist = []
  #numerator of cross correlation
  exoplist.append(sigmaplist[0]*sigmamlist[0]*sigmaplist[1]*sigmamlist[1])
  #denomenator of cross correlation
  exoplist.append(sigmaplist[0]*sigmamlist[0])
  exoplist.append(sigmaplist[1]*sigmamlist[1])

在此之后 下面这些代码不是 运行宁和花时间。不知道为什么?

   #initial state vector
  psi0 = basis(2,0)
  for i in range(1,n):
  psi0 = tensor(psi0,basis(2,0))
  #build Hamiltonian
  g2 = []
  list = np.linspace(0,200,2000)
  ntraj = 1
  while delta <= 7:
  glist = []
  hlist = []
  vlist = []
  for i in range(n):
        hlist.append(-delta*(sigmaplist[i]*sigmamlist[i])+ (omega / 2)*(sigmaplist[i] + 
        sigmamlist[i]))
  for i in range(n):
  for j in range(i):
        vlist.append((V / (n - 1))*((sigmaplist[j]*sigmamlist[j])
   *(sigmaplist[i]*sigmamlist[i])))
   H = sum(hlist) + sum(vlist)
   #monte carlo trajectory
   montecar = mcsolve(H, psi0, tlist, coplist, exoplist, ntraj)
   for i in range(len(montecar.expect[0])):
   numerator = montecar.expect[0][i]
  denomenator = montecar.expect[1][i]
  denom2 = montecar.expect[2][i]
  glist.append(numerator/(denomenator*denom2))
  g2.append(sum(glist)/len(glist))
  dlist.append(delta)
  delta = delta + deltastep
  x = np.arange(10)
  fig = plt.figure()
  ax = plt.subplot(111)
  ax.plot(dlist, g2)
  # Shink current axis by 20%
  box = ax.get_position()
  ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
  plt.grid(True)
  plt.show()   

你必须使用缩进,smt。像这样:

for j in range (n):
    if j==0:
        sigmajm=sigmam()
    else:
        sigmajm=qeye(2)

for i in range (1,n):
    if j == i:
        sigmaj1= tensor(sigmam(),sigmajm)
    else:
        sigmajm = tensor(qeye(2),sigmajm)
        sigmamlist.append(sigmajm)

更新: https://www.tutorialspoint.com/python/python_basic_syntax.htm