def get_trace(self, channel): """Get a trace. Args: channel (int): Which channel to get. Returns: (ValueArray[s]): Time axis. (ValueArray[V]): Voltage axis. """ word_length = 2 yield self.write(':WAV:SOUR CHAN{}'.format(channel)) yield self.write(':WAV:MSBF') yield self.write(':SING') preamble_raw = yield self.query(':WAV:PRE?') preamble = parse_preamble(preamble_raw) yield self.write(':WAV:FORM WORD') yield self.write(':WAV:DATA?') wave_bin = yield self.read_raw() wave_raw = parse_binary_waveform(wave_bin, word_length=word_length) y_incriment = preamble['y_incriment'] y_origin = preamble['y_origin'] y_reference = preamble['y_reference'] wave_volts = ((wave_raw - y_reference) * y_incriment) + y_origin x_incriment = preamble['x_incriment'] x_origin = preamble['x_origin'] num_points = preamble['num_points'] time_s = np.linspace(x_origin, x_origin + (num_points - 1) * x_incriment, num_points) returnValue((time_s * U.Unit('s'), wave_volts * U.Unit('V')))
def dep_coupling_factor_no_s(eta_c, N, d): """Coupling factor for two qubits, ignoring the self-inductance adjustment Args: eta_c (float): Coupling efficiency, i.e. the ratio M/L where M is the mutual inductance and L is the self inductance. N (int): Number of qubits you wish to couple to a single qubit. d (np.ndarray): Structured data array, with named columns. Returns (float): The dimensionless part of the qubit-qubit coupling. Multiply by omega_LC to get a coupling frequency. The qubit-qubit coupling is written J / hbar = [s (eta_c / N) (R_K / (8 pi Z_LC)) <0|phi|1>^2] omega_LC The part in [..] is the "coupling factor", a dimensionless parameter. The s factor is defined as (1 - M^2 / (L1 L2))^-1 and is near unity, so we ignore it. See the annealer-handbook for a full development of the coupling strength formula. """ Z_LC = d['Z_LC'] * U.Unit(LABELS['Z_LC'][1]) f_LC = d['f_LC'] * U.Unit(LABELS['f_LC'][1]) phi_10 = d['phi_10'] return (eta_c / N) * (R_K / (8 * np.pi * Z_LC)) * phi_10**2
def handleUnitConversion(self): """ Note that we only do unit conversion for the (shared) X-axis. """ import labrad.units as U newUnit = str(self.xAxisUnitsLE.text()).strip() currentUnit = U.Unit(self.xAxisCurrentUnit) originalUnit = U.Unit(self.variables[0][0][1]) if not newUnit: newUnit = self.variables[0][0][1] # if we change units, convert old data if newUnit != self.xAxisCurrentUnit: try: conversion = currentUnit.conversionTupleTo(newUnit) self.data[:, 0] = (self.data[:, 0] + conversion[1]) * conversion[0] if self.xAxisZero is not None: self.xAxisZero = (self.xAxisZero + conversion[1]) * conversion[0] self.xAxisCurrentUnit = newUnit for figure in self.figures.values(): figure.axes[0].set_xlabel( '%s [%s]' % (self.variables[0][0][0], self.xAxisCurrentUnit)) self.dirtyPlots = True except TypeError: pass # run new data through conversion if self.newData is not None and self.newData.shape[0] > 0: conversion = originalUnit.conversionTupleTo(self.xAxisCurrentUnit) self.newData[:, 0] = (self.newData[:, 0] + conversion[1]) * conversion[0]
def T1_us(d, M): f_LC = d['f_LC'][0] * U.Unit(LABELS['f_LC'][1]) Z_LC = d['Z_LC'][0] * U.Unit(LABELS['Z_LC'][1]) f_10 = d['f_10'][0] * U.Unit(LABELS['f_LC'][1]) phi_10 = d['phi_10'][0] R = 50 * Ohm Q = 4 * np.pi * (R / R_K) * (Z_LC / (2 * np.pi * f_LC * M))**2 / (phi_10**2) return (Q / (2 * np.pi * f_10))['us']
def testInfNan(self): ms = units.Unit('ms') GHz = units.Unit('GHz') MHz = units.Unit('MHz') self.assertEquals(float('inf') * GHz, float('inf') * MHz) self.assertNotEqual(float('inf') * GHz, float('inf') * ms) self.assertNotEqual(float('inf') * GHz, -float('inf') * GHz) self.assertNotEqual(float('nan') * GHz, float('nan') * GHz) self.assertNotEqual(float('nan') * GHz, float('nan') * ms)
def testArithmetic(self): m = units.Unit('m') kg = units.Unit('kg') self.assertEquals(units.Value(5.0, None)*m, 5.0*m) # addition self.assertEquals(1.0*kg + 0.0, 1.0*kg) self.assertNotEquals(1.0*kg, None)
def testTypeConversions(self): m = units.Unit('m') V = units.Unit('V') GHz = units.Unit('GHz') x1 = 1.0 * m x2 = 5j * V a = np.arange(10) * 1.0 va = units.ValueArray(np.arange(10) * 1.0, 'GHz') # Unit times number self.assertIsInstance(1.0 * m, units.Value) self.assertIsInstance(1 * m, units.Value) self.assertIsInstance(m * 1.0, units.Value) self.assertIsInstance(m * 1, units.Value) # Value times value or number self.assertIsInstance(x1 * x1, units.Value) self.assertIsInstance(x1 * 5, units.Value) self.assertIsInstance(0 * x1, units.Value) # Unit times complex self.assertIsInstance((1 + 1j) * V, units.Complex) self.assertIsInstance(V * (1 + 1j), units.Complex) # Value times Complex/complex self.assertIsInstance(x1 * 1j, units.Complex) self.assertIsInstance(1j * x1, units.Complex) self.assertIsInstance(x2 * x1, units.Complex) self.assertIsInstance(x1 * x2, units.Complex) # Unit/Value/ValueArray times array self.assertIsInstance(x1 * a, units.ValueArray) self.assertIsInstance(x2 * a, units.ValueArray) self.assertIsInstance(GHz * a, units.ValueArray) self.assertIsInstance(va * a, units.ValueArray) # Unit/Value/ValueArray times ValueArray self.assertIsInstance(x1 * va, units.ValueArray) self.assertIsInstance(x2 * va, units.ValueArray) self.assertIsInstance(GHz * va, units.ValueArray) self.assertIsInstance(va * va, units.ValueArray) # array times ? self.assertIsInstance(a * x1, units.ValueArray) self.assertIsInstance(a * x2, units.ValueArray) self.assertIsInstance(a * GHz, units.ValueArray) self.assertIsInstance(a * va, units.ValueArray) # ValueArray times ? self.assertIsInstance(va * x1, units.ValueArray) self.assertIsInstance(va * x2, units.ValueArray) self.assertIsInstance(va * GHz, units.ValueArray) self.assertIsInstance(va * a, units.ValueArray)
def testDimensionless(self): ns = units.Unit('ns') GHz = units.Unit('GHz') self.assertTrue(isinstance((5 * ns) * (5 * GHz), float)) self.assertTrue(hasattr((5 * ns) * (5 * GHz), 'inUnitsOf')) self.assertTrue(((5 * ns) * (5 * GHz)).isDimensionless()) self.assertTrue((5 * ns) * (5 * GHz) < 50) self.assertTrue( isinstance(units.WithUnit(5.0, ''), units.DimensionlessFloat)) self.assertTrue((5 * ns * 5j * GHz) == 25j) self.assertTrue((5 * ns * 5j * GHz).isDimensionless())
def dep_tunnelling_time_times_f_LC(which, d): """Tunneling time normalized to f_LC Args: d (np.ndarray): Structured data array with named columns. Returns (float): Tunneling time multiplied by f_LC. """ f_LC = d['f_LC'] * U.Unit(LABELS['f_LC'][1]) Z_LC = d['Z_LC'] * U.Unit(LABELS['Z_LC'][1]) phi_well = d[which] phi_barrier = d['phi_max'] return tunnel_time_times_f_LC(f_LC, Z_LC, phi_well, phi_barrier, d['beta'], d['phi_x'])
def testPickling(self): ns = units.Unit('ns') GHz = units.Unit('GHz') blank = units.Unit('') def round_trip(obj): return pickle.loads(pickle.dumps(obj)) self.assertEqual(round_trip(5 * GHz), 5 * GHz) # Value self.assertEqual(round_trip(GHz), GHz) # Unit self.assertTrue((round_trip(np.arange(5) * ns) == np.arange(5) * ns).all()) # array self.assertEqual(round_trip(5 * GHz * ns), 5) # Dimensionless self.assertIsInstance(round_trip(3 * blank), type( 3 * blank)) # Don't loose dimensionless type
def testArithmetic(self): m = units.Unit('m') kg = units.Unit('kg') km = units.Unit('km') #self.assertEqual(units.Value(5.0, None)*m, 5.0*m) # addition self.assertEqual(1.0 * kg + 0.0, 1.0 * kg) with self.assertRaises(TypeError): _ = 1.0 * kg + 1.0 * m with self.assertRaises(TypeError): _ = 1.0 * kg + 2.0 self.assertAlmostEqual(1.0 * km / m + 5.0, 1005) self.assertNotEqual(1.0 * kg, None)
def dep_number_of_states(which, d): """Number of states in a well Args: which (string): 'phi_left' or 'phi_right', indicating which well to analyze. d (np.ndarray): Structured data array with named columns. Returns (float): Number of states in the well. """ f_LC = d['f_LC'] * U.Unit(LABELS['f_LC'][1]) Z_LC = d['Z_LC'] * U.Unit(LABELS['Z_LC'][1]) phi = d[which] return number_of_states(f_LC, Z_LC, phi, d['phi_max'], d['phi_x'], d['beta'])
def horiz_position(self, position=None): horiz_scale = yield self.horiz_scale() if position is not None: pos = position * horiz_scale yield self.write(':TIM:POS {}'.format(-pos['s'])) resp = yield self.query(':TIM:POS?') returnValue(-float(resp) * U.Unit('s') / horiz_scale)
def testComplex(self): V = units.Unit('V') self.assertTrue(1j * V != 1.0 * V) self.assertTrue(1j * V == 1.0j * V) self.assertTrue(1.0 * V == (1 + 0j) * V) with self.assertRaises(TypeError): _ = 1.0j * V < 2j * V
def testComparison(self): s = units.Unit('s') ms = units.Unit('ms') kg = units.Unit('kg') self.assertTrue(1 * s > 10 * ms, '1*s > 10*ms') self.assertTrue(1 * s >= 10 * ms, '1*s >= 10*ms') self.assertTrue(1 * s < 10000 * ms, '1*s > 10000*ms') self.assertTrue(1 * s <= 10000 * ms, '1*s >= 10000*ms') self.assertTrue(10 * ms < 1 * s, '10*ms < 1*s') self.assertTrue(10 * ms <= 1 * s, '10*ms <= 1*s') self.assertTrue(10000 * ms > 1 * s, '10000*ms < 1*s') self.assertTrue(10000 * ms >= 1 * s, '10000*ms <= 1*s') with self.assertRaises(TypeError): nogood = 1 * s > 1 * kg self.assertFalse(1 * s == 1 * kg) self.assertTrue(0 * s == 0) self.assertTrue(4 * s > 0) with self.assertRaises(TypeError): _ = 4 * s > 1
def __le__(self, other): # this method is a bit funky. The <= relationship determines # which types are allowed to be coerced in flattening. If the # other type is also a value, we allow this if we have a unit, # or the other value does not. In other words, the only case # disallowed is the case where we have no unit but the other # type does. This prevents the unit from getting lost in the coercion. if type(other) == TAny: return True if type(self) != type(other): return False # If other is 'v', then any variant of 'v' is allowed. For example, if # we are 'v[]' or 'v[Hz]', we are allowed to pass to a setting # advertising 'v'. if other.unit is None: return True if self.unit is None: msg = "Unreachable: no python object should get %s as type"%(self,) raise TypeError(msg) # We have a unit, and the other guy has a unit, so make sure our units # are compatible. return U.Unit(self.unit).isCompatible(U.Unit(other.unit))
def sampling_rate(self, c, samplingRate=None): """ Set or get the sampling rate. Accepts: samplingRate: sampling rate in samples per unit time, or as a number assuming S/s units. Returns: samplingRate: sampling rate in samples per unit time. """ if samplingRate is None: if 'samplingRate' not in c: # Assume a sampling rate of 1 GS/s. c['samplingRate'] = 1000000000 else: if isinstance(samplingRate, units.Value): samplingRate = samplingRate['S/s'] rates = (1000000000, 500000000, 250000000) samplingRate = max( [rate for rate in rates if samplingRate >= rate]) c['samplingRate'] = samplingRate self.configure_clock_reference(c) return c['samplingRate'] * units.Unit('S/s')
def testNegativePowers(self): self.assertEqual(str(units.Unit('1/s')), 's^-1') self.assertEqual(str(units.Unit('1/s^1/2')), 's^-1/2')
def test_string_unit(self): ts = units.Unit('tshirt/s') self.assertEqual((1 * ts)['tshirt/h'], 3600.0) self.assertEqual(str(ts), 'tshirt/s')
def test_non_SI(self): units.addNonSI('count', True) x = 5 * units.Unit('kcount') self.assertTrue(x['count'] == 5000.0) self.assertTrue(x.inBaseUnits() == 5000.0 * units.Unit('count')) self.assertTrue((x**2).unit == units.Unit('kcount^2'))
def testNone(self): with self.assertRaises(Exception): units.Unit(None) with self.assertRaises(TypeError): None * units.Unit('MHz')
def testUnitPowers(self): self.assertTrue(units.Unit('ns')**2 == units.Unit('ns^2'))
def testBaseUnitPowers(self): x = Value(1, 'ns^2') self.assertTrue(x.unit.base_unit == units.Unit('s^2')) self.assertTrue(x.inBaseUnits() == Value(1e-18, 's^2'))
def testInUnitsOf(self): s = units.Unit('s') ms = units.Unit('ms') self.assertTrue((1 * s).inUnitsOf(ms) == 1000 * ms) self.assertTrue((1 * s).inUnitsOf('ms') == 1000 * ms)
def testUnitCreation(self): self.assertIsInstance( units.Unit('test0', 1.0, units.hplanck / (2 * units.e)), units.Unit) self.assertTrue( (units.Unit('phi0')**2).isCompatible(units.Unit('phi0^2')))
### END NODE INFO """ from datetime import datetime import math from twisted.python import log from twisted.internet.defer import inlineCallbacks, returnValue from labrad import types as T, util, units as U from labrad.server import setting from labrad.gpib import GPIBManagedServer, GPIBDeviceWrapper import labrad.units as units import numpy as np Ohm, K, s = [U.Unit(s) for s in ['Ohm', 'K', 's']] READ_ORDER = [1, 2, 1, 3, 1, 4, 1, 5] #N_CHANNELS = 5 DEFAULT_SETTLE_TIME = 8*s DEFAULT, FUNCTION, INTERPOLATION, VRHOPPING = range(4) # These functions suck. def res2temp(r): try: return units.K * ((math.log(r) - 6.02) / 1.76) ** (-1/.345) except Exception: return units.K*0.0 def temp2res(t): try:
import matplotlib matplotlib.use('Qt4Agg') import matplotlib.pyplot as plt import functools import numpy as np import scipy.interpolate as interpolate import scipy.optimize as opt import itertools import labrad import labrad.units as U from labrad.units import Value, ValueArray as VA us, GHz, Ohm, pH = (U.Unit(s) for s in ['us', 'GHz', 'Ohm', 'pH']) # Physical constants pi = np.pi PHI_0 = Value(2E-15, 'V*s') HBAR = Value(1.05E-34, 'J*s') R_K = Value(25.8E3, 'Ohm') # Data management NUM_BETAS = 10 NUM_PHI_POINTS = 200 DATA_NAMES = [ 'Z_LC', 'beta', 'f_LC', 'phi_x', 'f_10', 'f_21', 'phi_left', 'phi_right', 'phi_max', 'phi_10', '|0>', '|1>'
def testAngle(self): rad = units.Unit('rad') self.assertTrue(rad.is_angle) self.assertTrue(rad.isAngle()) x = units.Unit('rad*m/s') self.assertFalse(x.is_angle)
def dep(d): f_LC = d['f_LC'] * U.Unit(LABELS['f_LC'][1]) t = dep_tunnelling_time_times_f_LC(d) return t
def well_freq(d): f_LC = d['f_LC'] * U.Unit(LABELS['f_LC'][1]) Z_LC = d['Z_LC'] * U.Unit(LABELS['Z_LC'][1]) betas = d['beta'] f_well = f_LC * well_frequency_over_f_LC(d['phi_left'], betas) return f_well[LABELS['f_LC'][1]] / d['f_LC']