def test_quick_setup_2q(self): qubit_list = ['q0', 'q1'] with pytest.warns(UserWarning): # We did not provide any seed setup = quick_setup(qubit_list) assert 'q0' in setup.qubit_dic.keys() assert 'q1' in setup.qubit_dic.keys() assert len(setup.qubit_dic.keys()) == 2 for gate_name, gparams in setup.gate_dic.items(): if gparams['num_qubits'] == 2: assert (gate_name, 'q0', 'q1') in setup.gate_set assert (gate_name, 'q1', 'q0') in setup.gate_set for kw0, kw1 in gparams['circuit_args'].items(): if type(kw1) is not str: continue assert setup.qubit_dic['q0'][kw1] ==\ setup.gate_set[(gate_name, 'q0', 'q1')][0][kw0] assert setup.qubit_dic['q1'][kw1] ==\ setup.gate_set[(gate_name, 'q1', 'q0')][0][kw0] for kw0, kw1 in gparams['builder_args'].items(): if type(kw1) is not str: continue assert setup.qubit_dic['q0'][kw1] ==\ setup.gate_set[(gate_name, 'q0', 'q1')][1][kw0] assert setup.qubit_dic['q1'][kw1] ==\ setup.gate_set[(gate_name, 'q1', 'q0')][1][kw0]
def test_artificial_time(self): qubit_list = ['q0'] with pytest.warns(UserWarning): # We did not provide any seed setup = quick_setup(qubit_list) b = Builder(setup) b.add_gate('RY', ['q0'], angle=np.pi/2) b.add_gate('RY', ['q0'], angle=np.pi/2, time=0) assert b.times['q0'] == setup.gate_set[('RY', 'q0')][1]['gate_time'] assert b.circuit.gates[-1].time == 0
def test_simultaneous_gates(self): qubit_list = ['q0', 'q1'] with pytest.warns(UserWarning): # We did not provide any seed setup = quick_setup(qubit_list) b = Builder(setup) b < ('RY', 'q0', np.pi/2) b < (('RY', 'q0', np.pi/2), ('RY', 'q1', np.pi/2)) assert b.circuit.gates[-2].time == b.circuit.gates[-1].time assert b.times['q0'] == b.times['q1']
def test_save_load(self): rng = np.random.RandomState(42) qubit_list = ['q1', 'q2'] connectivity_dic = {'q1': ['q2'], 'q2': ['q1']} a = quick_setup(qubit_list=qubit_list, connectivity_dic=connectivity_dic, rng=rng) with tempfile.NamedTemporaryFile() as f: a.save(filename=f.name) b = Setup(filename=f.name, state=rng) for name in ('gate_dic', 'qubit_dic', 'gate_set'): assert a.__dict__[name] == b.__dict__[name] assert a.update_rules == b.update_rules
def test_quick_setup_1q(self): qubit_list = ['q_test'] with pytest.warns(UserWarning): # We did not provide any seed setup = quick_setup(qubit_list) assert hasattr(setup, 'gate_set') assert hasattr(setup, 'update_rules') assert hasattr(setup, 'qubit_dic') assert hasattr(setup, 'gate_dic') assert 'q_test' in setup.qubit_dic.keys() assert len(setup.qubit_dic.keys()) == 1 for gate_name, gparams in setup.gate_dic.items(): assert 'function' in gparams.keys() assert 'user_kws' in gparams.keys() if gparams['num_qubits'] == 1: assert (gate_name, 'q_test') in setup.gate_set for kw0, kw1 in gparams['circuit_args'].items(): if type(kw1) is not str: continue assert kw1 in setup.qubit_dic['q_test'] if gparams['num_qubits'] == 1: assert kw0 in setup.gate_set[(gate_name, 'q_test')][0] assert setup.qubit_dic['q_test'][kw1] ==\ setup.gate_set[(gate_name, 'q_test')][0][kw0] for kw0, kw1 in gparams['builder_args'].items(): if type(kw1) is not str: continue assert kw1 in setup.qubit_dic['q_test'] if gparams['num_qubits'] == 1: assert kw0 in setup.gate_set[(gate_name, 'q_test')][1] assert setup.qubit_dic['q_test'][kw1] ==\ setup.gate_set[(gate_name, 'q_test')][1][kw0]
def test_override(self): qubit_list = ['swap', 'cp'] with pytest.warns(UserWarning): # We did not provide any seed setup = quick_setup(qubit_list, noise_flag=False) b = Builder(setup) b < ('RotateY', 'swap', np.pi/2) b < ('RotateY', 'cp', np.pi/2) b < ('CZ', 'cp', 'swap') b < ('RotateY', 'cp', -np.pi/2) b.finalize() bell_circuit = b.circuit bell_state = SparseDM(bell_circuit.get_qubit_names()) bell_circuit.apply_to(bell_state) diag = np.diag(bell_state.full_dm.to_array()) assert np.abs(diag[0]-0.5) < 1e-10 assert np.abs(diag[3]-0.5) < 1e-10 assert np.abs(diag[1]) < 1e-10 assert np.abs(diag[2]) < 1e-10
def test_make_imperfect_bell(self): qubit_list = ['swap', 'cp'] with pytest.warns(UserWarning): # We did not provide any seed setup = quick_setup(qubit_list) b = Builder(setup) b.add_gate('RotateY', ['swap'], angle=np.pi/2) b.add_gate('RotateY', ['cp'], angle=np.pi/2) b.add_gate('CZ', ['cp', 'swap']) b.add_gate('RotateY', ['cp'], angle=-np.pi/2) b.finalize() bell_circuit = b.circuit bell_state = SparseDM(bell_circuit.get_qubit_names()) bell_circuit.apply_to(bell_state) diag = np.diag(bell_state.full_dm.to_array()) assert np.abs(diag[0]-0.5) < 1e-2 assert np.abs(diag[3]-0.5) < 1e-2 assert np.abs(diag[1]) < 3e-2 assert np.abs(diag[2]) < 3e-2
def test_qasm(self): qubit_list = ['swap', 'cp'] with pytest.warns(UserWarning): # We did not provide any seed setup = quick_setup(qubit_list, noise_flag=False) b = Builder(setup) qasm0 = 'Ry 1.57079632679 swap' qasm1 = 'Ry 1.57079632679 cp' qasm2 = 'CZ cp swap' qasm3 = 'Ry -1.57079632679 cp' qasm_list = [qasm0, qasm1, qasm2, qasm3] b.add_qasm(qasm_list, qubits_first=False) b.finalize() bell_circuit = b.circuit bell_state = SparseDM(bell_circuit.get_qubit_names()) bell_circuit.apply_to(bell_state) diag = np.diag(bell_state.full_dm.to_array()) assert np.abs(diag[0]-0.5) < 1e-10 assert np.abs(diag[3]-0.5) < 1e-10 assert np.abs(diag[1]) < 1e-10 assert np.abs(diag[2]) < 1e-10