def test_noisy_xy(): np.random.seed(15092021) simple_reg = Register.square(2, prefix="atom") detun = 1.0 amp = 3.0 rise = Pulse.ConstantPulse(1500, amp, detun, 0.0) simple_seq = Sequence(simple_reg, MockDevice) simple_seq.declare_channel("ch0", "mw_global") simple_seq.add(rise, "ch0") sim = Simulation(simple_seq, sampling_rate=0.01) with pytest.raises(NotImplementedError, match="mode 'XY' does not support simulation of"): sim.set_config(SimConfig(("SPAM", "doppler"))) sim.set_config(SimConfig("SPAM", eta=0.4)) assert sim._bad_atoms == { "atom0": True, "atom1": False, "atom2": True, "atom3": False, } with pytest.raises(NotImplementedError, match="simulation of noise types: amplitude"): sim.add_config(SimConfig("amplitude"))
def test_dephasing(): np.random.seed(123) reg = Register.from_coordinates([(0, 0)], prefix="q") seq = Sequence(reg, Chadoq2) seq.declare_channel("ch0", "rydberg_global") duration = 2500 pulse = Pulse.ConstantPulse(duration, np.pi, 0.0 * 2 * np.pi, 0) seq.add(pulse, "ch0") sim = Simulation(seq, sampling_rate=0.01, config=SimConfig(noise="dephasing")) assert sim.run().sample_final_state() == Counter({"0": 595, "1": 405}) assert len(sim._collapse_ops) != 0 with pytest.warns(UserWarning, match="first-order"): reg = Register.from_coordinates([(0, 0), (0, 10)], prefix="q") seq2 = Sequence(reg, Chadoq2) seq2.declare_channel("ch0", "rydberg_global") duration = 2500 pulse = Pulse.ConstantPulse(duration, np.pi, 0.0 * 2 * np.pi, 0) seq2.add(pulse, "ch0") sim = Simulation( seq2, sampling_rate=0.01, config=SimConfig(noise="dephasing", dephasing_prob=0.5), )
def test_config(): np.random.seed(123) reg = Register.from_coordinates([(0, 0), (0, 5)], prefix="q") seq = Sequence(reg, Chadoq2) seq.declare_channel("ch0", "rydberg_global") duration = 2500 pulse = Pulse.ConstantPulse(duration, np.pi, 0.0 * 2 * np.pi, 0) seq.add(pulse, "ch0") sim = Simulation(seq, config=SimConfig(noise="SPAM")) sim.reset_config() assert sim.config == SimConfig() sim.show_config() with pytest.raises(ValueError, match="not a valid"): sim.set_config("bad_config") clean_ham = sim.get_hamiltonian(123) new_cfg = SimConfig(noise="doppler", temperature=10000) sim.set_config(new_cfg) assert sim.config == new_cfg noisy_ham = sim.get_hamiltonian(123) assert (noisy_ham[0, 0] != clean_ham[0, 0] and noisy_ham[3, 3] == clean_ham[3, 3]) sim.set_config(SimConfig(noise="amplitude")) noisy_amp_ham = sim.get_hamiltonian(123) assert (noisy_amp_ham[0, 0] == clean_ham[0, 0] and noisy_amp_ham[0, 1] != clean_ham[0, 1])
def test_noise(): sim2 = Simulation(seq, sampling_rate=0.01, config=SimConfig(noise=("SPAM"), eta=0.4)) sim2.run() with pytest.raises(NotImplementedError, match="Cannot include"): sim2.set_config(SimConfig(noise="dephasing")) assert sim2.config.spam_dict == { "eta": 0.4, "epsilon": 0.01, "epsilon_prime": 0.05, }
def test_sample_final_state_noisy(): np.random.seed(123) assert results_noisy.sample_final_state(N_samples=1234) == Counter({ "00": 140, "01": 227, "10": 221, "11": 646 }) res_3level = Simulation(seq_no_meas_noisy, config=SimConfig(noise=("SPAM", "doppler"), runs=10)) final_state = res_3level.run().states[-1] assert np.isclose( final_state.full(), np.array([ [0.64 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j], [0.0 + 0.0j, 0.14 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j], [0.0 + 0.0j, 0.0 + 0.0j, 0.1 + 0.0j, 0.0 + 0.0j], [0.0 + 0.0j, 0.0 + 0.0j, 0.0 + 0.0j, 0.12 + 0.0j], ]), ).all()
def test_sample_final_state(): np.random.seed(123) sim_no_meas = Simulation(seq_no_meas, config=SimConfig(runs=1)) results_no_meas = sim_no_meas.run() assert results_no_meas.sample_final_state() == Counter({ "00": 88, "01": 156, "10": 188, "11": 568 }) with pytest.raises(NotImplementedError, match="dimension > 3"): results_large_dim = deepcopy(results) results_large_dim._dim = 7 results_large_dim.sample_final_state() sampling = results.sample_final_state(1234) assert len(sampling) == 4 # Check that all states were observed. results._meas_basis = "digital" sampling0 = results.sample_final_state(N_samples=911) assert sampling0 == {"00": 911} seq_no_meas.declare_channel("raman", "raman_local", "B") seq_no_meas.add(pi, "raman") res_3level = Simulation(seq_no_meas).run() # Raman pi pulse on one atom will not affect other, # even with global pi on rydberg assert len(res_3level.sample_final_state()) == 2 res_3level._meas_basis = "ground-rydberg" sampling_three_levelB = res_3level.sample_final_state() # Rydberg will affect both: assert len(sampling_three_levelB) == 4
def test_run(): sim = Simulation(seq, sampling_rate=0.01) sim.set_config(SimConfig("SPAM", eta=0.0)) with patch("matplotlib.pyplot.show"): with patch("matplotlib.pyplot.savefig"): sim.draw(draw_phase_area=True, fig_name="my_fig.pdf") bad_initial = np.array([1.0]) good_initial_array = np.r_[1, np.zeros(sim.dim**sim._size - 1)] good_initial_qobj = qutip.tensor( [qutip.basis(sim.dim, 0) for _ in range(sim._size)]) good_initial_qobj_no_dims = qutip.basis(sim.dim**sim._size, 2) with pytest.raises(ValueError, match="Incompatible shape of initial state"): sim.initial_state = bad_initial with pytest.raises(ValueError, match="Incompatible shape of initial state"): sim.initial_state = qutip.Qobj(bad_initial) sim.initial_state = good_initial_array sim.run() sim.initial_state = good_initial_qobj sim.run() sim.initial_state = good_initial_qobj_no_dims sim.run() seq.measure("ground-rydberg") sim.run() assert sim._seq._measurement == "ground-rydberg" sim.run(progress_bar=True) sim.run(progress_bar=False) sim.run(progress_bar=None) with pytest.raises( ValueError, match="`progress_bar` must be a bool.", ): sim.run(progress_bar=1) sim.set_config(SimConfig("SPAM", eta=0.1)) with pytest.raises( NotImplementedError, match="Can't combine state preparation errors with an initial state " "different from the ground.", ): sim.run()
def test_add_config(): reg = Register.from_coordinates([(0, 0)], prefix="q") seq = Sequence(reg, Chadoq2) seq.declare_channel("ch0", "rydberg_global") duration = 2500 pulse = Pulse.ConstantPulse(duration, np.pi, 0.0 * 2 * np.pi, 0) seq.add(pulse, "ch0") sim = Simulation(seq, sampling_rate=0.01, config=SimConfig(noise="SPAM", eta=0.5)) with pytest.raises(ValueError, match="is not a valid"): sim.add_config("bad_cfg") sim.add_config( SimConfig(noise=("dephasing", "SPAM", "doppler"), temperature=20000)) assert "dephasing" in sim.config.noise and "SPAM" in sim.config.noise assert sim.config.eta == 0.5 assert sim.config.temperature == 20000.0e-6 sim.set_config(SimConfig(noise="dephasing", laser_waist=175.0)) sim.add_config(SimConfig(noise=("SPAM", "amplitude"), laser_waist=172.0)) assert "amplitude" in sim.config.noise and "SPAM" in sim.config.noise assert sim.config.laser_waist == 172.0
def test_expect(): with pytest.raises(TypeError, match="must be a list"): results.expect("bad_observable") with pytest.raises(TypeError, match="Incompatible type"): results.expect(["bad_observable"]) with pytest.raises(ValueError, match="Incompatible shape"): results.expect([np.array(3)]) reg_single = Register.from_coordinates([(0, 0)], prefix="q") seq_single = Sequence(reg_single, Chadoq2) seq_single.declare_channel("ryd", "rydberg_global") seq_single.add(pi, "ryd") sim_single = Simulation(seq_single) results_single = sim_single.run() op = [qutip.basis(2, 0).proj()] exp = results_single.expect(op)[0] assert np.isclose(exp[-1], 1) assert len(exp) == duration + 1 # +1 for the final instant np.testing.assert_almost_equal( results_single._calc_pseudo_density(-1).full(), np.array([[1, 0], [0, 0]]), ) config = SimConfig(noise="SPAM", eta=0) sim_single.set_config(config) sim_single.evaluation_times = "Minimal" results_single = sim_single.run() exp = results_single.expect(op)[0] assert len(exp) == 2 assert isinstance(results_single, CoherentResults) assert results_single._meas_errors == { "epsilon": config.epsilon, "epsilon_prime": config.epsilon_prime, } # Probability of measuring 1 = probability of false positive assert np.isclose(exp[0], config.epsilon) # Probability of measuring 1 = 1 - probability of false negative assert np.isclose(exp[-1], 1 - config.epsilon_prime) np.testing.assert_almost_equal( results_single._calc_pseudo_density(-1).full(), np.array([[1 - config.epsilon_prime, 0], [0, config.epsilon_prime]]), ) seq3dim = Sequence(reg, Chadoq2) seq3dim.declare_channel("ryd", "rydberg_global") seq3dim.declare_channel("ram", "raman_local", initial_target="A") seq3dim.add(pi, "ram") seq3dim.add(pi, "ryd") sim3dim = Simulation(seq3dim) exp3dim = sim3dim.run().expect( [qutip.tensor(qutip.basis(3, 0).proj(), qutip.qeye(3))]) assert np.isclose(exp3dim[0][-1], 1.89690200e-14)
def test_get_hamiltonian(): simple_reg = Register.from_coordinates([[10, 0], [0, 0]], prefix="atom") detun = 1.0 rise = Pulse.ConstantDetuning(RampWaveform(1500, 0.0, 2.0), detun, 0.0) simple_seq = Sequence(simple_reg, Chadoq2) simple_seq.declare_channel("ising", "rydberg_global") simple_seq.add(rise, "ising") simple_sim = Simulation(simple_seq, sampling_rate=0.01) with pytest.raises(ValueError, match="less than or equal to"): simple_sim.get_hamiltonian(1650) with pytest.raises(ValueError, match="greater than or equal to"): simple_sim.get_hamiltonian(-10) # Constant detuning, so |rr><rr| term is C_6/r^6 - 2*detuning for any time simple_ham = simple_sim.get_hamiltonian(143) assert np.isclose(simple_ham[0, 0], Chadoq2.interaction_coeff / 10**6 - 2 * detun) np.random.seed(123) simple_sim_noise = Simulation(simple_seq, config=SimConfig(noise="doppler", temperature=20000)) simple_ham_noise = simple_sim_noise.get_hamiltonian(144) assert np.isclose( simple_ham_noise.full(), np.array([ [ 4.47984523 + 0.0j, 0.09606404 + 0.0j, 0.09606404 + 0.0j, 0.0 + 0.0j, ], [ 0.09606404 + 0.0j, 12.03082372 + 0.0j, 0.0 + 0.0j, 0.09606404 + 0.0j, ], [ 0.09606404 + 0.0j, 0.0 + 0.0j, -12.97113702 + 0.0j, 0.09606404 + 0.0j, ], [0.0 + 0.0j, 0.09606404 + 0.0j, 0.09606404 + 0.0j, 0.0 + 0.0j], ]), ).all()
def test_effective_size_disjoint(): simple_reg = Register.square(2, prefix="atom") rise = Pulse.ConstantPulse(1500, 0, 0, 0) for channel_type in ["mw_global", "rydberg_global", "raman_global"]: np.random.seed(15092021) seq = Sequence(simple_reg, MockDevice) seq.declare_channel("ch0", channel_type) seq.add(rise, "ch0") seq.config_slm_mask(["atom1"]) sim = Simulation(seq, sampling_rate=0.01) sim.set_config(SimConfig("SPAM", eta=0.4)) assert sim._bad_atoms == { "atom0": True, "atom1": False, "atom2": True, "atom3": False, } assert sim.get_hamiltonian(0) == 0 * sim.build_operator( [("I", "global")])
def test_get_final_state_noisy(): np.random.seed(123) seq_ = Sequence(reg, Chadoq2) seq_.declare_channel("ram", "raman_local", initial_target="A") seq_.add(pi, "ram") noisy_config = SimConfig(noise=("SPAM", "doppler")) sim_noisy = Simulation(seq_, config=noisy_config) res3 = sim_noisy.run() res3._meas_basis = "digital" final_state = res3.get_final_state() assert isdiagonal(final_state) res3._meas_basis = "ground-rydberg" assert (final_state[0, 0] == 0.06666666666666667 + 0j and final_state[2, 2] == 0.92 + 0j) assert res3.states[-1] == final_state assert res3.results[-1] == Counter({ "10": 0.92, "00": 0.06666666666666667, "11": 0.013333333333333334 })
def test_init(): config = SimConfig( noise=("SPAM", "doppler", "dephasing", "amplitude"), temperature=1000.0, runs=100, ) str_config = config.__str__(True) assert ("SPAM, doppler, dephasing, amplitude" in str_config and "1000.0µK" in str_config and "100" in str_config and "Solver Options" in str_config) with pytest.raises(ValueError, match="is not a valid noise type."): SimConfig(noise="bad_noise") with pytest.raises(ValueError, match="Temperature field"): SimConfig(temperature=-1.0) with pytest.raises(ValueError, match="SPAM parameter"): SimConfig(eta=-1.0)
def test_cuncurrent_pulses(): reg = Register({"q0": (0, 0)}) seq = Sequence(reg, Chadoq2) seq.declare_channel("ch_local", "rydberg_local", initial_target="q0") seq.declare_channel("ch_global", "rydberg_global") pulse = Pulse.ConstantPulse(20, 10, 0, 0) seq.add(pulse, "ch_local") seq.add(pulse, "ch_global", protocol="no-delay") # Clean simulation sim_no_noise = Simulation(seq) # Noisy simulation sim_with_noise = Simulation(seq) config_doppler = SimConfig(noise=("doppler")) sim_with_noise.set_config(config_doppler) for t in sim_no_noise.evaluation_times: ham_no_noise = sim_no_noise.get_hamiltonian(t) ham_with_noise = sim_with_noise.get_hamiltonian(t) assert ham_no_noise[0, 1] == ham_with_noise[0, 1]
reg = Register(q_dict) duration = 1000 pi = Pulse.ConstantDetuning(BlackmanWaveform(duration, np.pi), 0.0, 0) seq = Sequence(reg, Chadoq2) # Declare Channels seq.declare_channel("ryd", "rydberg_global") seq.add(pi, "ryd") seq_no_meas = deepcopy(seq) seq_no_meas_noisy = deepcopy(seq) seq.measure("ground-rydberg") sim = Simulation(seq) cfg_noisy = SimConfig(noise=("SPAM", "doppler", "amplitude")) sim_noisy = Simulation(seq, config=cfg_noisy) results = sim.run() results_noisy = sim_noisy.run() state = qutip.tensor([qutip.basis(2, 0), qutip.basis(2, 0)]) ground = qutip.tensor([qutip.basis(2, 1), qutip.basis(2, 1)]) def test_initialization(): with pytest.raises(ValueError, match="`basis_name` must be"): CoherentResults(state, 2, "bad_basis", None, [0]) with pytest.raises( ValueError, match="`meas_basis` must be 'ground-rydberg' or 'digital'."): CoherentResults(state, 1, "all", None, "XY")