def build_rb_circuit(nseeds=1, length_vector=None, rb_pattern=None, length_multiplier=1, seed_offset=0, align_cliffs=False, seed=None): """ Randomized Benchmarking sequences. """ if not seed: np.random.seed(10) else: np.random.seed(seed) rb_opts = {} rb_opts['nseeds'] = nseeds rb_opts['length_vector'] = length_vector rb_opts['rb_pattern'] = rb_pattern rb_opts['length_multiplier'] = length_multiplier rb_opts['seed_offset'] = seed_offset rb_opts['align_cliffs'] = align_cliffs # Generate the sequences try: rb_circs, _ = rb.randomized_benchmarking_seq(**rb_opts) except OSError: skip_msg = ('Skipping tests because ' 'tables are missing') raise NotImplementedError(skip_msg) all_circuits = [] for seq in rb_circs: all_circuits += seq return all_circuits
def rb_circuit_execution(rb_opts: dict, shots: int): """ Create rb circuits with depolarizing error and simulate them Args: rb_opts: the options for the rb circuits shots: number of shots for each circuit simulation Returns: list: list of Results of the circuits simulations list: the xdata of the rb circuit """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') basis_gates = ['u1', 'u2', 'u3', 'cx'] rb_circs, xdata = rb.randomized_benchmarking_seq(**rb_opts) noise_model = create_depolarizing_noise_model() results = [] for circuit in rb_circs: results.append( qiskit.execute(circuit, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) return results, xdata
def rb_purity_circuit_execution(rb_opts: dict, shots: int): """ Create purity rb circuits with depolarizing errors and simulate them Args: rb_opts: the options for the rb circuits shots: number of shots for each circuit simulation Returns: list: list of Results of the purity circuits simulations list: the xdata of the rb circuit int: npurity (3^(number of qubits)) list: list of Results of the coherent circuits simulations """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') basis_gates = ['u1', 'u2', 'u3', 'cx'] rb_purity_circs, xdata, npurity = rb.randomized_benchmarking_seq(**rb_opts) noise_model = create_depolarizing_noise_model() # coherent noise err_unitary = np.zeros([2, 2], dtype=complex) angle_err = 0.1 for i in range(2): err_unitary[i, i] = np.cos(angle_err) err_unitary[i, (i + 1) % 2] = np.sin(angle_err) err_unitary[0, 1] *= -1.0 error = coherent_unitary_error(err_unitary) coherent_noise_model = NoiseModel() coherent_noise_model.add_all_qubit_quantum_error(error, 'u3') purity_results = [] coherent_results = [] for circuit_list in rb_purity_circs: for purity_num in range(npurity): current_circ = circuit_list[purity_num] # non-coherent purity results purity_results.append( qiskit.execute(current_circ, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) # coherent purity results # THE FITTER IS NOT TESTED YET coherent_results.append( qiskit.execute(current_circ, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=coherent_noise_model, seed_simulator=SEED).result()) return purity_results, xdata, npurity, coherent_results
def rb_cnotdihedral_execution(rb_opts: dict, shots: int): """ Create cnot-dihedral rb circuits with depolarizing errors and simulates them Args: rb_opts: the options for the rb circuits shots: number of shots for each circuit simulation Returns: list: list of Results of the cnot-dihedral circuits simulations in the x plane list: the xdata of the rb circuit list: list of Results of the cnot-dihedral circuits simulations in the z plane """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') basis_gates = ['u1', 'u2', 'u3', 'cx'] rb_cnotdihedral_z_circs, xdata, rb_cnotdihedral_x_circs = \ rb.randomized_benchmarking_seq(**rb_opts) # Add depolarizing noise to the simulation noise_model = NoiseModel() p1q = 0.002 p2q = 0.01 noise_model.add_all_qubit_quantum_error(depolarizing_error(p1q, 1), 'u2') noise_model.add_all_qubit_quantum_error(depolarizing_error(2 * p1q, 1), 'u3') noise_model.add_all_qubit_quantum_error(depolarizing_error(p2q, 2), 'cx') cnotdihedral_x_results = [] for circuit in rb_cnotdihedral_x_circs: cnotdihedral_x_results.append( qiskit.execute(circuit, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) cnotdihedral_z_results = [] for circuit in rb_cnotdihedral_z_circs: cnotdihedral_z_results.append( qiskit.execute(circuit, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) return cnotdihedral_x_results, xdata, cnotdihedral_z_results
def rb_interleaved_execution(rb_opts: dict, shots: int): """ Create interleaved rb circuits with depolarizing error and simulates them Args: rb_opts: the options for the rb circuits shots: number of shots for each circuit simulation Returns: list: list of Results of the original circuits simulations list: the xdata of the rb circuit list: list of Results of the interleaved circuits simulations """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') basis_gates = ['u1', 'u2', 'u3', 'cx'] rb_original_circs, xdata, rb_interleaved_circs = rb.randomized_benchmarking_seq( **rb_opts) noise_model = NoiseModel() p1q = 0.002 p2q = 0.01 noise_model.add_all_qubit_quantum_error(depolarizing_error(p1q, 1), 'u2') noise_model.add_all_qubit_quantum_error(depolarizing_error(2 * p1q, 1), 'u3') noise_model.add_all_qubit_quantum_error(depolarizing_error(p2q, 2), 'cx') results = [] for circuit in rb_original_circs: results.append( qiskit.execute(circuit, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) interleaved_results = [] for circuit in rb_interleaved_circs: interleaved_results.append( qiskit.execute(circuit, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) return results, xdata, interleaved_results
def rb_circuit_execution_2(rb_opts: dict, shots: int): """ Create rb circuits with T1 and T2 errors and simulate them Args: rb_opts: the options for the rb circuits shots: number of shots for each circuit simulation Returns: list: list of Results of the circuits simulations list: the xdata of the rb circuit """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') basis_gates = ['u1', 'u2', 'u3', 'cx'] rb_circs, xdata = rb.randomized_benchmarking_seq(**rb_opts) noise_model = NoiseModel() # Add T1/T2 noise to the simulation t_1 = 100. t_2 = 80. gate1q = 0.1 gate2q = 0.5 noise_model.add_all_qubit_quantum_error( thermal_relaxation_error(t_1, t_2, gate1q), 'u2') noise_model.add_all_qubit_quantum_error( thermal_relaxation_error(t_1, t_2, 2 * gate1q), 'u3') noise_model.add_all_qubit_quantum_error( thermal_relaxation_error(t_1, t_2, gate2q).tensor( thermal_relaxation_error(t_1, t_2, gate2q)), 'cx') results = [] for circuit in rb_circs: results.append( qiskit.execute(circuit, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) return results, xdata
def test_rb(self, nq, pattern_type, multiplier_type): """ Main function of the test """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') # See documentation of choose_pattern for the meaning of # the different pattern types # Choose options for standard (simultaneous) RB: rb_opts = {} rb_opts['nseeds'] = 3 rb_opts['length_vector'] = [1, 3, 4, 7] rb_opts['rb_pattern'], is_purity = \ self.choose_pattern(pattern_type, nq) # if the pattern type is not relevant for nq if rb_opts['rb_pattern'] is None: raise unittest.SkipTest('pattern type is not relevant for nq') rb_opts_purity = rb_opts.copy() rb_opts['length_multiplier'] = self.choose_multiplier( multiplier_type, len(rb_opts['rb_pattern'])) # Choose options for interleaved RB: rb_opts_interleaved = rb_opts.copy() rb_opts_interleaved['interleaved_gates'] = \ self.choose_interleaved_gates(rb_opts['rb_pattern']) # Choose options for purity rb # no length_multiplier rb_opts_purity['length_multiplier'] = 1 rb_opts_purity['is_purity'] = is_purity if multiplier_type > 0: is_purity = False # Adding seed_offset and align_cliffs rb_opts['seed_offset'] = 10 rb_opts['align_cliffs'] = True # Generate the sequences try: # Standard (simultaneous) RB sequences: rb_circs, _ = rb.randomized_benchmarking_seq(**rb_opts) # Interleaved RB sequences: rb_original_circs, _, rb_interleaved_circs = \ rb.randomized_benchmarking_seq( **rb_opts_interleaved) # Purity RB sequences: if is_purity: rb_purity_circs, _, npurity = \ rb.randomized_benchmarking_seq( **rb_opts_purity) # verify: npurity = 3^n self.assertEqual( npurity, 3 ** len(rb_opts['rb_pattern'][0]), 'Error: npurity does not equal to 3^n') except OSError: skip_msg = ('Skipping tests for %s qubits because ' 'tables are missing' % str(nq)) raise unittest.SkipTest(skip_msg) # Perform an ideal execution on the generated sequences basis_gates = ['u1', 'u2', 'u3', 'cx'] shots = 100 result = [] result_original = [] result_interleaved = [] if is_purity: result_purity = [[] for d in range(npurity)] for seed in range(rb_opts['nseeds']): result.append( qiskit.execute(rb_circs[seed], backend=backend, basis_gates=basis_gates, shots=shots).result()) result_original.append( qiskit.execute(rb_original_circs[seed], backend=backend, basis_gates=basis_gates, shots=shots).result()) result_interleaved.append( qiskit.execute(rb_interleaved_circs[seed], backend=backend, basis_gates=basis_gates, shots=shots).result()) if is_purity: for d in range(npurity): result_purity[d].append(qiskit.execute( rb_purity_circs[seed][d], backend=backend, basis_gates=basis_gates, shots=shots).result()) # Verify the generated sequences for seed in range(rb_opts['nseeds']): length_vec = rb_opts['length_vector'] for circ_index, vec_len in enumerate(length_vec): # Verify circuits names self.assertEqual( rb_circs[seed][circ_index].name, 'rb_length_%d_seed_%d' % ( circ_index, seed + rb_opts['seed_offset']), 'Error: incorrect circuit name') self.assertEqual( rb_original_circs[seed][circ_index].name, 'rb_length_%d_seed_%d' % ( circ_index, seed), 'Error: incorrect circuit name') self.assertEqual( rb_interleaved_circs[seed][circ_index].name, 'rb_interleaved_length_%d_seed_%d' % ( circ_index, seed), 'Error: incorrect interleaved circuit name') if is_purity: for d in range(npurity): name_type, _ = self.update_purity_gates( npurity, d, rb_opts_purity ['rb_pattern']) self.assertEqual( rb_purity_circs[seed][d] [circ_index].name, 'rb_purity_%s_length_%d_seed_%d' % ( name_type, circ_index, seed), 'Error: incorrect purity circuit name') self.verify_circuit(rb_circs[seed][circ_index], nq, rb_opts, vec_len, result[seed], shots) self.verify_circuit(rb_original_circs[seed] [circ_index], nq, rb_opts, vec_len, result_original[seed], shots) self.verify_circuit(rb_interleaved_circs[seed] [circ_index], nq, rb_opts_interleaved, vec_len, result_interleaved[seed], shots, is_interleaved=True) if is_purity: self.verify_circuit(rb_purity_circs[seed][0] [circ_index], nq, rb_opts_purity, vec_len, result_purity [0][seed], shots) # compare the purity RB circuits # with the original circuit for d in range(1, npurity): self.compare_purity_circuits( rb_purity_circs[seed][0][circ_index], rb_purity_circs[seed][d][circ_index], nq, d, npurity, rb_opts_purity, vec_len) # compare the interleaved RB circuits with # the original RB circuits self.compare_interleaved_circuit( rb_original_circs[seed][circ_index], rb_interleaved_circs[seed][circ_index], nq, rb_opts_interleaved, vec_len) self.assertEqual(circ_index, len(rb_circs), "Error: additional circuits exist")
#number of qubits nQ=2 rb_opts = {} #Number of Cliffords in the sequence rb_opts['length_vector'] = [1, 51, 75, 100, 125, 150, 175, 200] n_cliff = len(rb_opts['length_vector']) #Number of seeds (random sequences) rb_opts['nseeds'] = 5 n_seed = 5 #Default pattern rb_opts['rb_pattern'] = [[0, 1]] from qiskit.ignis.verification.randomized_benchmarking import randomized_benchmarking_seq import qiskit.ignis.verification.randomized_benchmarking as rb #create randomized benchmarking circuits with 5 seeds rb_circuits_seeds, xdata = randomized_benchmarking_seq(**rb_opts) rb_circuits_seeds[0][0].draw() # ┌───┐ ┌───┐ ┌───┐ ┌─────┐┌───┐ ░ ┌───┐┌───┐ ┌─────┐┌───┐┌───┐┌─┐ #qr_0: ┤ Y ├─┤ H ├─┤ S ├──■──┤ SDG ├┤ H ├─░─┤ H ├┤ S ├──■──┤ SDG ├┤ H ├┤ Y ├┤M├─── # ├───┤┌┴───┴┐├───┤┌─┴─┐├─────┤├───┤ ░ ├───┤├───┤┌─┴─┐└┬───┬┘├───┤├───┤└╥┘┌─┐ #qr_1: ┤ Y ├┤ SDG ├┤ H ├┤ X ├┤ SDG ├┤ H ├─░─┤ H ├┤ S ├┤ X ├─┤ H ├─┤ S ├┤ Y ├─╫─┤M├ # └───┘└─────┘└───┘└───┘└─────┘└───┘ ░ └───┘└───┘└───┘ └───┘ └───┘└───┘ ║ └╥┘ #cr: 2/══════════════════════════════════════════════════════════════════════╩══╩═ # 0 1 # Create a new circuit without the measurement qregs = rb_circuits_seeds[0][-1].qregs cregs = rb_circuits_seeds[0][-1].cregs qc0 = qiskit.QuantumCircuit(*qregs, *cregs)
# Number of seeds (random sequences) rb_opts['nseeds'] = 10 # Default pattern rb_opts['rb_pattern'] = [[0, 1]] shots = 8192 # backend_names = ['ibmq_qasm_simulator' , 'ibmq_athens', 'ibmq_santiago', 'ibmq_quito', 'ibmq_lima', 'ibmq_belem'] backend_names = ['ibmq_qasm_simulator'] """ Configuration End """ log = get_logger("Evaluate") # Generate RB circuits (2Q RB) rb_circs, xdata = rb.randomized_benchmarking_seq(**rb_opts) count = 0 for listElem in rb_circs: count += len(listElem) log.info(f"Generated {count} circuits") provider = IBMQ.load_account() input_pipeline = Queue() input_exec = Queue() output_exec = Queue() agg_results = Queue() output_pipline = Queue()
def test_rb(self): """ Main function of the test """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') # Test up to 2 qubits nq_list = [1, 2] for nq in nq_list: print("Testing %d qubit RB" % nq) for pattern_type in range(2): for multiplier_type in range(2): # See documentation of choose_pattern for the meaning of # the different pattern types # Choose options for standard (simultaneous) RB: rb_opts = {} rb_opts['nseeds'] = 3 rb_opts['length_vector'] = [1, 3, 4, 7] rb_opts['rb_pattern'] = self.choose_pattern( pattern_type, nq) # if the pattern type is not relevant for nq if rb_opts['rb_pattern'] is None: continue rb_opts['length_multiplier'] = self.choose_multiplier( multiplier_type, len(rb_opts['rb_pattern'])) # Choose options for interleaved RB: rb_opts_interleaved = rb_opts.copy() rb_opts_interleaved['interleaved_gates'] = \ self.choose_interleaved_gates(rb_opts['rb_pattern']) # Generate the sequences try: # Standard (simultaneous) RB sequences: rb_circs, _ = rb.randomized_benchmarking_seq(**rb_opts) # Interleaved RB sequences: rb_original_circs, _, rb_interleaved_circs = \ rb.randomized_benchmarking_seq( **rb_opts_interleaved) except OSError: skip_msg = ('Skipping tests for %s qubits because ' 'tables are missing' % str(nq)) print(skip_msg) continue # Perform an ideal execution on the generated sequences basis_gates = ['u1', 'u2', 'u3', 'cx'] shots = 100 result = [] result_original = [] result_interleaved = [] for seed in range(rb_opts['nseeds']): result.append( qiskit.execute(rb_circs[seed], backend=backend, basis_gates=basis_gates, shots=shots).result()) result_original.append( qiskit.execute(rb_original_circs[seed], backend=backend, basis_gates=basis_gates, shots=shots).result()) result_interleaved.append( qiskit.execute(rb_interleaved_circs[seed], backend=backend, basis_gates=basis_gates, shots=shots).result()) # Verify the generated sequences for seed in range(rb_opts['nseeds']): length_vec = rb_opts['length_vector'] for circ_index, vec_len in enumerate(length_vec): self.assertEqual( rb_circs[seed][circ_index].name, 'rb_length_%d_seed_%d' % (circ_index, seed), 'Error: incorrect circuit name') self.assertEqual( rb_original_circs[seed][circ_index].name, 'rb_length_%d_seed_%d' % (circ_index, seed), 'Error: incorrect circuit name') self.assertEqual( rb_interleaved_circs[seed][circ_index].name, 'rb_interleaved_length_%d_seed_%d' % (circ_index, seed), 'Error: incorrect circuit name') self.verify_circuit(rb_circs[seed][circ_index], nq, rb_opts, vec_len, result[seed], shots) self.verify_circuit( rb_original_circs[seed][circ_index], nq, rb_opts, vec_len, result_original[seed], shots) self.verify_circuit( rb_interleaved_circs[seed][circ_index], nq, rb_opts_interleaved, vec_len, result_interleaved[seed], shots, is_interleaved=True) # compare the interleaved RB circuits with # the original RB circuits self.compare_interleaved_circuit( rb_original_circs[seed][circ_index], rb_interleaved_circs[seed][circ_index], nq, rb_opts_interleaved, vec_len) self.assertEqual(circ_index, len(rb_circs), "Error: additional circuits exist")
def test_rb(self): """ Main function of the test """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') # Test up to 2 qubits nq_list = [1, 2] for nq in nq_list: print("Testing %d qubit RB" % nq) for pattern_type in range(2): for multiplier_type in range(2): # See documentation of choose_pattern for the meaning of # the different pattern types rb_opts = {} rb_opts['nseeds'] = 3 rb_opts['length_vector'] = [1, 3, 4, 7] rb_opts['rb_pattern'] = self.choose_pattern( pattern_type, nq) # if the pattern type is not relevant for nq if rb_opts['rb_pattern'] is None: continue rb_opts['length_multiplier'] = self.choose_multiplier( multiplier_type, len(rb_opts['rb_pattern'])) # Generate the sequences try: rb_circs, _ = rb.randomized_benchmarking_seq(**rb_opts) except OSError: skip_msg = ('Skipping tests for %s qubits because ' 'tables are missing' % str(nq)) print(skip_msg) continue # Perform an ideal execution on the generated sequences # basis_gates = ['u1','u2','u3','cx'] # use U, CX for now # Shelly: changed format to fit qiskit current version basis_gates = 'u1, u2, u3, cx' shots = 100 result = [] for seed in range(rb_opts['nseeds']): result.append( qiskit.execute(rb_circs[seed], backend=backend, basis_gates=basis_gates, shots=shots).result()) # Verify the generated sequences for seed in range(rb_opts['nseeds']): length_vec = rb_opts['length_vector'] for circ_index, vec_len in enumerate(length_vec): self.assertEqual( rb_circs[seed][circ_index].name, 'rb_length_%d_seed_%d' % ( circ_index, seed), 'Error: incorrect circuit name') self.verify_circuit(rb_circs[seed][circ_index], nq, rb_opts, vec_len, result[seed], shots) self.assertEqual(circ_index, len(rb_circs), "Error: additional circuits exist")