def test_use_modes(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) with system.use_modes([qubit]): self.assertEqual(system.ground_state(), qubit.fock(0, full_space=False)) with system.use_modes([cavity]): self.assertEqual(system.ground_state(), cavity.fock(0, full_space=False)) with system.use_modes([qubit, cavity]): self.assertEqual( system.ground_state(), qutip.tensor( qubit.fock(0, full_space=False), cavity.fock(0, full_space=False) ), ) system.order_modes = False with system.use_modes([cavity, qubit]): self.assertEqual( system.ground_state(), qutip.tensor( cavity.fock(0, full_space=False), qubit.fock(0, full_space=False) ), )
def test_coupling_terms_multimode(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity1 = Cavity("cavity1", levels=10, kerr=-10e-6) cavity2 = Cavity("cavity2", levels=6, kerr=-10e-6) system = System("system", modes=[qubit, cavity2, cavity1]) system.set_cross_kerr(qubit, cavity1, -2e-3) system.set_cross_kerr(qubit, cavity2, -1e-3) system.set_cross_kerr(cavity1, cavity2, -5e-6) self.assertEqual(len(system.couplings()), 3) for modes in [[qubit, cavity1], [qubit, cavity1, cavity2]]: with system.use_modes(modes): self.assertEqual( system.couplings()[0], 2 * np.pi * -2e-3 * qubit.n * cavity1.n ) for i, modes in enumerate([[qubit, cavity2], [qubit, cavity1, cavity2]]): with system.use_modes(modes): self.assertEqual( system.couplings()[i], 2 * np.pi * -1e-3 * qubit.n * cavity2.n ) for i, modes in enumerate([[cavity2, cavity1], [qubit, cavity1, cavity2]]): with system.use_modes(modes): self.assertEqual( system.couplings()[2 * i], 2 * np.pi * -5e-6 * cavity1.n * cavity2.n )
def test_c_ops(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) # [ # qubit.decay, qubit.excitation, qubit.dephasing, # cavity.decay, cavity.excitation, cavity.dephasing, # ] self.assertEqual(len(system.c_ops(clean=False)), 6) self.assertEqual(len(system.c_ops(clean=True)), 0) qubit.t1 = 100e3 cavity.t2 = 500e3 # [qubit.decay, cavity.dephasing] self.assertEqual(len(system.c_ops(clean=True)), 2)
def test_to_from_dict(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) system.set_cross_kerr(qubit, cavity, chi=-2e-3) other_system = System.from_dict(system.as_dict()) self.assertEqual(system, other_system)
def test_to_from_json_str(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) system.set_cross_kerr(qubit, cavity, chi=-2e-3) json_str = system.to_json(dumps=True) other_system = System.from_json(json_str=json_str) self.assertEqual(system, other_system)
def setUpClass(cls): qubit = Transmon("qubit", levels=2) cavity = Cavity("cavity", levels=6) system = System("system", modes=[qubit, cavity]) system.set_cross_kerr(qubit, cavity, -2e-3) with system.use_modes([qubit]): _ = tune_rabi(system, init_state=qubit.fock(0), verify=False) cls.system = system
def test_fock(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) with self.assertRaises(ValueError): system.fock(0) with self.assertRaises(ValueError): system.fock(0, qubit=0)
def test_get_mode(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) self.assertIs(system.get_mode(qubit), qubit) self.assertIs(system.get_mode("qubit"), qubit) with self.assertRaises(ValueError): system.get_mode("other_qubit")
def test_to_from_json_file(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) system.set_cross_kerr(qubit, cavity, chi=-2e-3) with tempfile.TemporaryDirectory() as dirname: json_path = os.path.join(dirname, "__test_to_from_json_file.json") system.to_json(json_path=json_path) other_system = System.from_json(json_path=json_path) self.assertEqual(system, other_system)
def test_coupling_terms(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) chi = -2e-3 key = frozenset([qubit.name, cavity.name]) system.coupling_terms[key].append( CouplingTerm(qubit, "n", cavity, "n", strength=2 * np.pi * chi) ) self.assertEqual(len(system.couplings()), 1) self.assertEqual(system.couplings()[0], 2 * np.pi * chi * qubit.n * cavity.n) system.set_cross_kerr(qubit, cavity, chi) self.assertEqual(len(system.couplings()), 1) self.assertEqual(system.couplings()[0], 2 * np.pi * chi * qubit.n * cavity.n)
def test_displacement(self): c0 = Cavity("c0", levels=10, kerr=-10e-6) c1 = Cavity("c1", levels=12, kerr=-10e-6) system = System("system", modes=[c0, c1]) init_state = system.fock() for cavity in [c0, c1]: for _ in range(1): _ = tune_displacement(system, init_state, mode_name=cavity.name, verify=False) for alpha in [0, 1, -1, 1j, -1j, 2, -2, 2j, -2j]: ideal_state = cavity.tensor_with_zero( qutip.coherent(cavity.levels, alpha)) seq = get_sequence(system) unitary = cavity.displace(alpha, unitary=True) cavity.displace(alpha) result = seq.run(init_state) unitary_fidelity = (qutip.fidelity(unitary * init_state, ideal_state)**2) pulse_fidelity = qutip.fidelity(result.states[-1], ideal_state)**2 self.assertGreater(unitary_fidelity, 1 - 1e-4) self.assertGreater(pulse_fidelity, 1 - 1e-4)
def test_active_modes(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit]) system.active_modes = [qubit] with self.assertRaises(ValueError): system.active_modes = [qubit, cavity] system.modes = [qubit, cavity] system.active_modes = [qubit, cavity] system.modes = [cavity] with self.assertRaises(ValueError): _ = system.active_modes
def test_displacement(self): cavity = Cavity("cavity", levels=12) system = System("system", modes=[cavity]) for _ in range(3): _, old_amp, new_amp = tune_displacement(system, cavity.fock(0)) self.assertLess(abs(old_amp - new_amp), 1e-7) init = cavity.fock(0) seq = get_sequence(system) cavity.displace(1 + 2j) result = seq.run(init) target = cavity.D(1 + 2j) * init fidelity = qutip.fidelity(result.states[-1], target)**2 self.assertLess(abs(1 - fidelity), 1e-7)
def test_H0(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) system.set_cross_kerr(qubit, cavity, 0) # [qubit.self_kerr, cavity.self_kerr] self.assertEqual(len(system.H0(clean=True)), 2) # [ # qubit.self_kerr, cavity.self_kerr, # qubit.detuning, cavity.detuning, # qubit-cavity cross-Kerr # ] self.assertEqual(len(system.H0(clean=False)), 5) system.set_cross_kerr(qubit, cavity, -2e-3) # [qubit.self_kerr, cavity.self_kerr, qubit-cavity cross-Kerr] self.assertEqual(len(system.H0(clean=True)), 3)
def test_order_modes(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) system.order_modes = True system.active_modes = [qubit, cavity] self.assertEqual(system.modes, [qubit, cavity]) self.assertEqual(system.active_modes, [qubit, cavity]) system.active_modes = [cavity, qubit] self.assertEqual(system.modes, [qubit, cavity]) self.assertEqual(system.active_modes, [qubit, cavity]) system = System("system", modes=[qubit, cavity]) system.order_modes = False system.active_modes = [qubit, cavity] self.assertEqual(system.modes, [qubit, cavity]) self.assertEqual(system.active_modes, [qubit, cavity]) system.active_modes = [cavity, qubit] self.assertEqual(system.modes, [qubit, cavity]) self.assertEqual(system.active_modes, [cavity, qubit])
def test_set_cross_kerr(self): qubit = Transmon("qubit", levels=3, kerr=-200e-3) cavity = Cavity("cavity", levels=10, kerr=-10e-6) system = System("system", modes=[qubit, cavity]) with self.assertRaises(ValueError): system.set_cross_kerr(qubit, qubit, -1e-3) chi = -2e-3 system.set_cross_kerr(qubit, cavity, chi) self.assertEqual(len(system.couplings()), 1) self.assertEqual(system.couplings()[0], 2 * np.pi * chi * qubit.n * cavity.n) system.set_cross_kerr(qubit, cavity, chi) self.assertEqual(len(system.couplings()), 1) self.assertEqual(system.couplings()[0], 2 * np.pi * chi * qubit.n * cavity.n) system.set_cross_kerr(cavity, qubit, chi) self.assertEqual(len(system.couplings()), 1) self.assertEqual(system.couplings()[0], 2 * np.pi * chi * qubit.n * cavity.n)
def test_invalid_mode_type(self): cavities = [Cavity(f"c{i}", levels=3, kerr=-2e-3) for i in range(5)] _ = System("system", modes=cavities) with self.assertRaises(TypeError): _ = gates.x(*cavities, unitary=False)
def test_paulis_custom_logical_states(self): c0 = Cavity("c0", levels=6) c1 = Cavity("c1", levels=10) c0.set_logical_states(c0.basis(0), c0.basis(1)) c1.set_logical_states(c1.basis(2), c1.basis(3)) c0_0 = c0.logical_zero(full_space=False) c0_1 = c0.logical_one(full_space=False) c1_0 = c1.logical_zero(full_space=False) c1_1 = c1.logical_one(full_space=False) c0_I = c0.I c1_I = c1.I single_mode_paulis = { "X0": c0_0 * c0_1.dag() + c0_1 * c0_0.dag(), "X1": c1_0 * c1_1.dag() + c1_1 * c1_0.dag(), "Y0": -1j * (c0_0 * c0_1.dag() - c0_1 * c0_0.dag()), "Y1": -1j * (c1_0 * c1_1.dag() - c1_1 * c1_0.dag()), "Z0": c0_0 * c0_0.dag() - c0_1 * c0_1.dag(), "Z1": c1_0 * c1_0.dag() - c1_1 * c1_1.dag(), } c0.space = c1.space = [c1, c0] self.assertEqual(c0.sigmax(full_space=False), single_mode_paulis["X0"]) self.assertEqual(c0.sigmay(full_space=False), single_mode_paulis["Y0"]) self.assertEqual(c0.sigmaz(full_space=False), single_mode_paulis["Z0"]) self.assertEqual(c1.sigmax(full_space=False), single_mode_paulis["X1"]) self.assertEqual(c1.sigmay(full_space=False), single_mode_paulis["Y1"]) self.assertEqual(c1.sigmaz(full_space=False), single_mode_paulis["Z1"]) self.assertEqual(c0.sigmax(full_space=True), qutip.tensor(c1_I, single_mode_paulis["X0"])) self.assertEqual(c0.sigmay(full_space=True), qutip.tensor(c1_I, single_mode_paulis["Y0"])) self.assertEqual(c0.sigmaz(full_space=True), qutip.tensor(c1_I, single_mode_paulis["Z0"])) self.assertEqual(c1.sigmax(full_space=True), qutip.tensor(single_mode_paulis["X1"], c0_I)) self.assertEqual(c1.sigmay(full_space=True), qutip.tensor(single_mode_paulis["Y1"], c0_I)) self.assertEqual(c1.sigmaz(full_space=True), qutip.tensor(single_mode_paulis["Z1"], c0_I))
def test_set_logical_states_none(self): c0 = Cavity("c0", levels=6) c1 = Cavity("c1", levels=10) c0.set_logical_states(None, None) c1.set_logical_states(None, None) self.assertEqual(c0.logical_zero(), c0.basis(0)) self.assertEqual(c0.logical_one(), c0.basis(1)) self.assertEqual(c1.logical_zero(), c1.basis(0)) self.assertEqual(c1.logical_one(), c1.basis(1))
def test_paulis_fock(self): q0 = Cavity("q0", levels=6) q1 = Cavity("q1", levels=10) q0_0 = q0.basis(0, full_space=False) q0_1 = q0.basis(1, full_space=False) q1_0 = q1.basis(0, full_space=False) q1_1 = q1.basis(1, full_space=False) q0_I = q0.I q1_I = q1.I single_mode_paulis = { "X0": q0_0 * q0_1.dag() + q0_1 * q0_0.dag(), "X1": q1_0 * q1_1.dag() + q1_1 * q1_0.dag(), "Y0": -1j * (q0_0 * q0_1.dag() - q0_1 * q0_0.dag()), "Y1": -1j * (q1_0 * q1_1.dag() - q1_1 * q1_0.dag()), "Z0": q0_0 * q0_0.dag() - q0_1 * q0_1.dag(), "Z1": q1_0 * q1_0.dag() - q1_1 * q1_1.dag(), } q0.space = q1.space = [q1, q0] self.assertEqual(q0.sigmax(full_space=False), single_mode_paulis["X0"]) self.assertEqual(q0.sigmay(full_space=False), single_mode_paulis["Y0"]) self.assertEqual(q0.sigmaz(full_space=False), single_mode_paulis["Z0"]) self.assertEqual(q1.sigmax(full_space=False), single_mode_paulis["X1"]) self.assertEqual(q1.sigmay(full_space=False), single_mode_paulis["Y1"]) self.assertEqual(q1.sigmaz(full_space=False), single_mode_paulis["Z1"]) self.assertEqual(q0.sigmax(full_space=True), qutip.tensor(q1_I, single_mode_paulis["X0"])) self.assertEqual(q0.sigmay(full_space=True), qutip.tensor(q1_I, single_mode_paulis["Y0"])) self.assertEqual(q0.sigmaz(full_space=True), qutip.tensor(q1_I, single_mode_paulis["Z0"])) self.assertEqual(q1.sigmax(full_space=True), qutip.tensor(single_mode_paulis["X1"], q0_I)) self.assertEqual(q1.sigmay(full_space=True), qutip.tensor(single_mode_paulis["Y1"], q0_I)) self.assertEqual(q1.sigmaz(full_space=True), qutip.tensor(single_mode_paulis["Z1"], q0_I))
def test_invalid_logical_state(self): c0 = Cavity("c0", levels=6) c1 = Cavity("c1", levels=10) c0.set_logical_states(c0.basis(0), c0.basis(1)) c1.set_logical_states(c1.basis(2), c1.basis(3)) c0.space = c1.space = [c1, c0] with self.assertRaises(ValueError): c0.set_logical_states(c0.basis(0), c0.basis(1)) with self.assertRaises(ValueError): c1.set_logical_states(c1.basis(2), c1.basis(3))