AI-assisted opamp design

AMPIC Lab, University of Delaware (Prof. Vishal Saxena)

In this notebook an AI loop sizes a two-stage Miller-compensated opamp from a specification. You will see how to

  1. describe the circuit with a fast physics-based model (gm/ID design),
  2. let a small neural network learn the circuit from a few hundred simulations,
  3. search the design space on the neural network instead of the simulator,
  4. verify every candidate on the real model and feed the result back (active learning),
  5. compare against random search and a classic optimizer, counting simulator calls.

The circuit model here is a compact, illustrative 180 nm-class model so that everything runs in seconds, in your browser, with no installation. At the end we show how to swap in a real SPICE simulator.

To run: use Runtime > Run all in Google Colab.

In [1]:
import numpy as np, matplotlib.pyplot as plt, time
from scipy.optimize import differential_evolution
from scipy.stats import qmc
from sklearn.neural_network import MLPRegressor
from sklearn.preprocessing import StandardScaler
plt.rcParams.update({'figure.dpi':110,'axes.grid':True,'grid.alpha':.3,'font.size':10})

1. The circuit and its model

A PMOS-input differential pair (M1, M2) with an NMOS current-mirror load drives an NMOS common-source second stage (M6) with a PMOS current source. A capacitor Cc in series with a resistor Rz gives Miller compensation.

The designer's knobs (the design variables) follow the gm/ID methodology: bias currents, the inversion level of each stage (gm/ID), channel lengths, the compensation capacitor, and the nulling resistor (as a multiple of 1/gm2).

simulate(x) returns DC gain, gain-bandwidth (unity-gain frequency), phase margin, power, slew rate and area for any batch of designs. The poles and the zero are computed from the small-signal model, and the phase margin comes from the full transfer function.

In [2]:
# ---- illustrative 180 nm-class constants (not a foundry model) ----
VDD=1.8; UT=0.02585; N=1.3
KP_N, KP_P = 250e-6, 70e-6      # uCox [A/V^2]
VA_N, VA_P = 5.0, 6.0           # Early voltage per micron of L [V/um]
COX=8.5e-15; CJ=0.8e-15; CL=2e-12
NAMES=['I1 (uA)','I2 (uA)','gm/Id M1','gm/Id M6','L1 (um)','L2 (um)','Cc (pF)','Rz*gm2']
LO=np.array([5,  20, 6,  6, 0.18,0.18,0.2,0.5])
HI=np.array([200,800,22, 18, 2.0, 1.5, 5.0,2.5])

def wl(gmid,I,kp):                      # W/L from gm/ID using the EKV inversion coefficient
    g=gmid*N*UT; ic=np.maximum((1/g-0.5)**2-0.25,1e-3)
    return I/(ic*2*N*kp*UT**2)

def small_signal(x):
    x=np.atleast_2d(x); I1,I2,g1,g2,L1,L2,Cc,k=[x[:,i] for i in range(8)]
    I1=I1*1e-6; I2=I2*1e-6; Cc=Cc*1e-12; Id1=I1/2
    gm1=g1*Id1; gm2=g2*I2
    W1=wl(g1,Id1,KP_P)*L1; W6=wl(g2,I2,KP_N)*L2; W3=wl(8.0,Id1,KP_N)*L1
    ro2=VA_P*L1/Id1; ro4=VA_N*L1/Id1; R1=ro2*ro4/(ro2+ro4)
    ro6=VA_N*L2/I2;  ro7=VA_P*L2/I2;  R2=ro6*ro7/(ro6+ro7)
    A0=gm1*R1*gm2*R2
    C1=(2/3)*COX*W6*L2+CJ*(W1+W3)*2
    C2=CL+CJ*(W6+W6*KP_N/KP_P)
    p1=1/(gm2*R1*R2*Cc)
    p2=gm2*Cc/(C1*C2+Cc*(C1+C2))
    z =1/(Cc*(1/gm2-k/gm2))
    return dict(I1=I1,I2=I2,Cc=Cc,W1=W1,W3=W3,W6=W6,L1=L1,L2=L2,A0=A0,p1=p1,p2=p2,z=z)

def simulate(x):
    """Batch 'simulator': (n,8) design variables -> dict of performance metrics."""
    s=small_signal(x); f=np.logspace(3,10.5,700); w=1j*2*np.pi*f[None,:]
    H=s['A0'][:,None]*(1-w/s['z'][:,None])/((1+w/s['p1'][:,None])*(1+w/s['p2'][:,None]))
    mag=np.abs(H); ph=np.unwrap(np.angle(H),axis=1)
    below=mag<1; idx=np.where(below.any(1),below.argmax(1),len(f)-1)
    return dict(gain_db=20*np.log10(s['A0']), gbw_hz=f[idx],
                pm_deg=180+np.degrees(ph[np.arange(len(H)),idx]),
                power_w=VDD*(s['I1']+s['I2']+s['I1']/2), sr=s['I1']/s['Cc'],
                area_um2=2*s['W1']*s['L1']+2*s['W3']*s['L1']+2*s['W6']*s['L2']+s['Cc']/2e-15)

def bode(x):
    s=small_signal(x); f=np.logspace(2,10.5,800); w=1j*2*np.pi*f
    H=s['A0'][0]*(1-w/s['z'][0])/((1+w/s['p1'][0])*(1+w/s['p2'][0]))
    return f,20*np.log10(np.abs(H)),np.degrees(np.unwrap(np.angle(H)))

A first design by hand

Try a reasonable guess and look at the Bode plot. Change the numbers and re-run to build intuition.

In [3]:
x0=np.array([[40, 200, 12, 10, 1.0, 0.5, 1.0, 1.2]])   # I1, I2, gm/Id1, gm/Id2, L1, L2, Cc, Rz*gm2
m=simulate(x0)
print({k:float(np.round(v[0],3)) for k,v in m.items()})
f,mag,ph=bode(x0)
fig,ax=plt.subplots(2,1,figsize=(6.5,5),sharex=True)
ax[0].semilogx(f,mag); ax[0].set_ylabel('Gain (dB)'); ax[0].axhline(0,c='k',lw=.8)
ax[1].semilogx(f,ph); ax[1].set_ylabel('Phase (deg)'); ax[1].set_xlabel('Frequency (Hz)')
plt.tight_layout(); plt.show()
{'gain_db': 52.992, 'gbw_hz': 37223349.552, 'pm_deg': 73.704, 'power_w': 0.0, 'sr': 40000000.0, 'area_um2': 637.893}
No description has been provided for this image

2. The specification

We want an opamp with gain at least 70 dB, unity-gain frequency at least 20 MHz, phase margin at least 60 degrees, power at most 1 mW and slew rate at least 10 V/us, with the smallest area.

score() combines area with a penalty for every violated spec. It is the single number the optimizers try to minimize.

In [4]:
SPEC=dict(gain_db=70.0, gbw_hz=20e6, pm_deg=60.0, power_w=1e-3, sr=10e6)

def violation(m,spec):
    r=lambda a:np.maximum(0,a)
    return (r((spec['gain_db']-m['gain_db'])/spec['gain_db'])+r((spec['gbw_hz']-m['gbw_hz'])/spec['gbw_hz'])
           +r((spec['pm_deg']-m['pm_deg'])/spec['pm_deg'])+r((m['power_w']-spec['power_w'])/spec['power_w'])
           +r((spec['sr']-m['sr'])/spec['sr']))

n_sim=0                                  # counts every call of the "real" simulator
def true_score(x,spec):
    global n_sim
    x=np.atleast_2d(x); n_sim+=len(x); m=simulate(x)
    return m['area_um2']/1000+200*violation(m,spec)

def lhs(n,seed): return qmc.scale(qmc.LatinHypercube(d=8,seed=seed).random(n),LO,HI)

X=lhs(20000,0); m=simulate(X); ok=violation(m,SPEC)==0
print(f"Random designs that meet the spec: {ok.sum()} of {len(X)}  ({100*ok.mean():.2f} %)")
Random designs that meet the spec: 135 of 20000  (0.68 %)

3. Baselines: random search and a classic optimizer

Random search throws darts. Differential evolution (DE) is a strong, widely used evolutionary optimizer. Both call the simulator for every candidate, so we count the calls.

In [5]:
def random_search(spec,n,seed=1):
    X=lhs(n,seed); sc=true_score(X,spec); i=sc.argmin(); return X[i],sc[i]

def de_direct(spec,seed=0,maxiter=60):
    f=lambda x: true_score(x.T,spec)
    r=differential_evolution(f,list(zip(LO,HI)),maxiter=maxiter,popsize=15,tol=1e-8,seed=seed,vectorized=True,updating='deferred',polish=False)
    return r.x,r.fun

res={}
for n in (400,2000,10000):
    n_sim=0; x,sc=random_search(SPEC,n); res[f'Random search ({n})']=(sc,n_sim,x)
n_sim=0; xde,sde=de_direct(SPEC); res['Differential evolution']=(sde,n_sim,xde)
for k,(sc,ns,_) in res.items(): print(f"{k:28s} score {sc:7.3f}   simulator calls {ns:6d}")
Random search (400)          score   5.227   simulator calls    400
Random search (2000)         score   2.302   simulator calls   2000
Random search (10000)        score   2.065   simulator calls  10000
Differential evolution       score   0.529   simulator calls   7320

4. Teach a neural network the circuit

Instead of asking the simulator every time, we simulate a few hundred well-spread designs (Latin hypercube sampling) and train a small neural network surrogate that predicts all metrics from the design variables.

The network is a multilayer perceptron. Its inputs are the design variables (in log scale), and its outputs are gain, log(GBW), phase margin, log(slew rate), log(power) and log(area).

In [6]:
def to_Y(m): return np.column_stack([m['gain_db'],np.log10(m['gbw_hz']),m['pm_deg'],np.log10(m['sr']),np.log10(m['power_w']),np.log10(m['area_um2'])])
def from_Y(Y): return dict(gain_db=Y[:,0],gbw_hz=10**Y[:,1],pm_deg=Y[:,2],sr=10**Y[:,3],power_w=10**Y[:,4],area_um2=10**Y[:,5])

class Surrogate:
    def fit(self,X,Y):
        self.sx=StandardScaler().fit(np.log(X)); self.sy=StandardScaler().fit(Y)
        self.net=MLPRegressor(hidden_layer_sizes=(64,64),activation='tanh',max_iter=1500,
                              learning_rate_init=3e-3,alpha=1e-5,random_state=0).fit(self.sx.transform(np.log(X)),self.sy.transform(Y))
        return self
    def predict(self,X):
        return from_Y(self.sy.inverse_transform(self.net.predict(self.sx.transform(np.log(np.atleast_2d(X))))))

n_sim=0
Xtr=lhs(300,7); n_sim+=len(Xtr); Ytr=to_Y(simulate(Xtr))
sur=Surrogate().fit(Xtr,Ytr)

Xte=lhs(2000,123); mt=simulate(Xte); mp=sur.predict(Xte)       # unseen designs
fig,ax=plt.subplots(1,3,figsize=(11,3.4))
for a,(key,lab,log) in zip(ax,[('gain_db','Gain (dB)',0),('pm_deg','Phase margin (deg)',0),('gbw_hz','GBW (Hz)',1)]):
    a.scatter(mt[key],mp[key],s=3,alpha=.4); lo_,hi_=min(mt[key].min(),mp[key].min()),max(mt[key].max(),mp[key].max())
    a.plot([lo_,hi_],[lo_,hi_],'k',lw=.8); a.set_xlabel('True '+lab); a.set_ylabel('Neural net')
    if log: a.set_xscale('log'); a.set_yscale('log')
    r2=1-np.sum((mt[key]-mp[key])**2)/np.sum((mt[key]-mt[key].mean())**2); a.set_title(f'R$^2$ = {r2:.3f}')
plt.tight_layout(); plt.show()
print('Simulator calls used to train:',n_sim)
No description has been provided for this image
Simulator calls used to train: 300

5. The AI loop: search on the network, verify on the simulator

Searching the network costs nothing, so we let DE explore it freely. But a surrogate is only accurate where it has seen data, so we never trust it blindly:

  1. Find the best design on the surrogate.
  2. Run that design (and a few random perturbations of it) on the real simulator.
  3. Add the true results to the training set and retrain, so the surrogate gets sharper exactly where it matters.
  4. Repeat.

This is a simple form of active learning. Results vary a little with the random seed, but a typical run lands close to the classic optimizer (sometimes better) with a small fraction of the simulator calls. Try other seeds to see the spread.

In [7]:
def ai_design(spec,n0=300,rounds=10,k=15,seed=0,verbose=True):
    global n_sim
    n_sim=0; rng=np.random.default_rng(seed)
    X=lhs(n0,seed); Y=to_Y(simulate(X)); n_sim+=n0; hist=[]
    for r in range(rounds):
        s=Surrogate().fit(X,Y)
        obj=lambda x: (lambda m: m['area_um2']/1000+200*violation(m,spec))(s.predict(x.T))
        d=differential_evolution(obj,list(zip(LO,HI)),maxiter=150,popsize=25,tol=1e-8,seed=seed+r,vectorized=True,updating='deferred',polish=False)
        cand=np.vstack([d.x]+[np.clip(d.x*np.exp(0.08*rng.standard_normal(8)),LO,HI) for _ in range(k-1)])
        Yc=to_Y(simulate(cand)); n_sim+=len(cand)
        X=np.vstack([X,cand]); Y=np.vstack([Y,Yc])
        sc=np.min(from_Y(Y)['area_um2']/1000+200*violation(from_Y(Y),spec)); hist.append((n_sim,sc))
        if verbose: print(f"round {r+1}: simulator calls {n_sim:4d}   best true score {sc:.3f}")
    sc_all=from_Y(Y)['area_um2']/1000+200*violation(from_Y(Y),spec); i=sc_all.argmin()
    return X[i],sc_all[i],n_sim,hist

t=time.time(); xai,sai,nai,hist=ai_design(SPEC); print(f"\nAI loop finished in {time.time()-t:.1f} s")
res['AI loop (surrogate + verify)']=(sai,nai,xai)
round 1: simulator calls  315   best true score 2.465
round 2: simulator calls  330   best true score 2.465
round 3: simulator calls  345   best true score 2.465
round 4: simulator calls  360   best true score 0.674
round 5: simulator calls  375   best true score 0.672
round 6: simulator calls  390   best true score 0.618
round 7: simulator calls  405   best true score 0.618
round 8: simulator calls  420   best true score 0.618
round 9: simulator calls  435   best true score 0.603
round 10: simulator calls  450   best true score 0.603

AI loop finished in 3.3 s

6. Results

In [8]:
print(f"{'Method':32s} {'score':>8s} {'sim calls':>10s}   (score = area/1000 when the spec is met)")
for k,(sc,ns,_) in res.items(): print(f"{k:32s} {sc:8.3f} {ns:10d}")

names=list(res); fig,ax=plt.subplots(figsize=(7,3.2))
cols=['#999']*3+['#2F6FA8','#E05149']
ax.barh(names,[res[k][1] for k in names],color=cols); ax.set_xscale('log'); ax.set_xlabel('Simulator calls (log scale)'); ax.invert_yaxis()
for i,k in enumerate(names): ax.text(res[k][1]*1.08,i,f"score {res[k][0]:.2f}",va='center',fontsize=8)
ax.set_xlim(100,1e5); plt.tight_layout(); plt.show()
Method                              score  sim calls   (score = area/1000 when the spec is met)
Random search (400)                 5.227        400
Random search (2000)                2.302       2000
Random search (10000)               2.065      10000
Differential evolution              0.529       7320
AI loop (surrogate + verify)        0.603        450
No description has been provided for this image
In [9]:
def report(x,spec,title='Best design'):
    m=simulate(x); x=np.atleast_2d(x)[0]
    print(title); print('-'*len(title))
    for n,v in zip(NAMES,x): print(f"  {n:10s} {v:9.3f}")
    rows=[('Gain (dB)',m['gain_db'][0],'>= %.0f'%spec['gain_db']),('GBW (MHz)',m['gbw_hz'][0]/1e6,'>= %.0f'%(spec['gbw_hz']/1e6)),
          ('Phase margin (deg)',m['pm_deg'][0],'>= %.0f'%spec['pm_deg']),('Power (mW)',m['power_w'][0]*1e3,'<= %.2f'%(spec['power_w']*1e3)),
          ('Slew rate (V/us)',m['sr'][0]/1e6,'>= %.0f'%(spec['sr']/1e6)),('Area (um^2)',m['area_um2'][0],'minimize')]
    print(); [print(f"  {a:20s} {b:9.2f}   spec {c}") for a,b,c in rows]
    f,mag,ph=bode(x)
    fig,ax=plt.subplots(2,1,figsize=(6.5,5),sharex=True)
    ax[0].semilogx(f,mag); ax[0].axhline(0,c='k',lw=.8); ax[0].set_ylabel('Gain (dB)')
    ax[0].axvline(m['gbw_hz'][0],c='#E05149',ls='--'); ax[1].semilogx(f,ph); ax[1].set_ylabel('Phase (deg)'); ax[1].set_xlabel('Frequency (Hz)')
    ax[1].axhline(-180+spec['pm_deg'],c='gray',ls=':'); ax[1].axvline(m['gbw_hz'][0],c='#E05149',ls='--')
    ax[0].set_title(f"{title}: GBW {m['gbw_hz'][0]/1e6:.0f} MHz, PM {m['pm_deg'][0]:.0f} deg"); plt.tight_layout(); plt.show()
report(xai,SPEC,'AI-designed opamp')
AI-designed opamp
-----------------
  I1 (uA)       11.294
  I2 (uA)       47.026
  gm/Id M1      15.612
  gm/Id M6      14.097
  L1 (um)        1.939
  L2 (um)        1.075
  Cc (pF)        0.569
  Rz*gm2         2.487

  Gain (dB)                70.66   spec >= 70
  GBW (MHz)                20.07   spec >= 20
  Phase margin (deg)       61.43   spec >= 60
  Power (mW)                0.12   spec <= 1.00
  Slew rate (V/us)         19.85   spec >= 10
  Area (um^2)             603.10   spec minimize
No description has been provided for this image

7. A tougher specification

Tighten the spec and run the AI loop again. Because the surrogate is reused as a map of the design space, a new spec costs only another few hundred simulations.

In [10]:
SPEC2=dict(gain_db=74.0, gbw_hz=40e6, pm_deg=65.0, power_w=1.5e-3, sr=20e6)
x2,s2,n2,_=ai_design(SPEC2,seed=3,verbose=False)
print(f"Tougher spec: score {s2:.3f} with {n2} simulator calls")
report(x2,SPEC2,'AI-designed opamp, tougher spec')
Tougher spec: score 1.997 with 450 simulator calls
AI-designed opamp, tougher spec
-------------------------------
  I1 (uA)      103.218
  I2 (uA)       20.118
  gm/Id M1      13.466
  gm/Id M6      17.814
  L1 (um)        1.896
  L2 (um)        1.492
  Cc (pF)        1.173
  Rz*gm2         2.469

  Gain (dB)                74.07   spec >= 74
  GBW (MHz)                40.09   spec >= 40
  Phase margin (deg)       65.69   spec >= 65
  Power (mW)                0.31   spec <= 1.50
  Slew rate (V/us)         87.97   spec >= 20
  Area (um^2)            1996.98   spec minimize
No description has been provided for this image

8. Try your own spec (interactive)

Use the sliders and press Run. (Works in Colab and Jupyter.)

In [11]:
try:
    import ipywidgets as W
    from IPython.display import display
    g=W.FloatSlider(value=70,min=50,max=80,step=1,description='Gain dB'); b=W.FloatLogSlider(value=20e6,base=10,min=6.5,max=8.3,step=.02,description='GBW Hz')
    p=W.FloatSlider(value=60,min=45,max=80,step=1,description='PM deg'); pw=W.FloatLogSlider(value=1e-3,base=10,min=-4,max=-2.3,step=.02,description='Power W')
    s=W.FloatLogSlider(value=10e6,base=10,min=5.5,max=8,step=.02,description='SR V/s'); go=W.Button(description='Run AI design',button_style='success'); out=W.Output()
    def run(_):
        out.clear_output(wait=True)
        with out:
            sp=dict(gain_db=g.value,gbw_hz=b.value,pm_deg=p.value,power_w=pw.value,sr=s.value)
            x,sc,ns,_=ai_design(sp,seed=5,verbose=False); print(f"{ns} simulator calls; score {sc:.3f}")
            report(x,sp,'Your spec')
    go.on_click(run); display(W.VBox([g,b,p,pw,s,go,out]))
except Exception as e:
    print('ipywidgets not available:',e)

From toy to real

Everything above treats simulate(x) as a black box. To design a real circuit, replace it with a call to a circuit simulator and keep the rest unchanged:

  • Open-source flow: generate a netlist from x, run ngspice or Xyce with an open PDK such as SkyWater sky130, and parse gain, GBW and phase margin from the AC analysis.
  • Industry flow: the same loop works with Cadence Spectre through its Python interface.
  • Agentic extension: an LLM agent can propose the topology, write the netlist, read simulator error messages, fix them, and then hand the numeric sizing to the loop above. The agent handles the discrete decisions, and the surrogate handles the continuous ones.
  • Beyond sizing: the same idea applies to layout (parasitic-aware re-sizing) and to photonic circuits.

Limits of this demo: the compact model ignores noise, offset, mismatch, process corners and layout parasitics. A production flow must verify across PVT corners and with post-layout extraction.

Questions or interested in this research? See the AMPIC Lab or email vsaxena@udel.edu.