예제 #1
0
 def test_interleaved_rb_experiment(self, interleaved_element: "Gate",
                                    qubits: list):
     """
     Initializes data and executes an interleaved RB experiment with specific parameters.
     Args:
         interleaved_element: The Clifford element to interleave
         qubits (list): A list containing qubit indices for the experiment
     """
     backend = AerSimulator.from_backend(FakeParis())
     exp_attributes = {
         "interleaved_element": interleaved_element,
         "physical_qubits": qubits,
         "lengths": [1, 4, 6, 9, 13, 16],
         "num_samples": 3,
         "seed": 100,
     }
     rb_exp = InterleavedRB(
         exp_attributes["interleaved_element"],
         exp_attributes["physical_qubits"],
         exp_attributes["lengths"],
         num_samples=exp_attributes["num_samples"],
         seed=exp_attributes["seed"],
     )
     experiment_obj = rb_exp.run(backend)
     exp_data = experiment_obj.experiment
     exp_circuits = rb_exp.circuits()
     self.validate_metadata(exp_circuits, exp_attributes)
     self.validate_circuit_data(exp_data, exp_attributes)
     self.is_identity(exp_circuits)
 def _load_rb_data(self, rb_exp_data_file_name: str):
     """
     loader for the experiment data and configuration setup.
     Args:
         rb_exp_data_file_name(str): The file name that contain the experiment data.
     Returns:
         list: containing dict of the experiment setup configuration and list of dictionaries
             containing the experiment results.
         ExperimentData:  ExperimentData object that was creates by the analysis function.
     """
     interleaved_gates = {"x": XGate(), "cx": CXGate()}
     data, exp_attributes, expdata1 = self._load_json_data(rb_exp_data_file_name)
     rb_exp = InterleavedRB(
         interleaved_gates[exp_attributes["interleaved_element"]],
         exp_attributes["physical_qubits"],
         exp_attributes["lengths"],
         num_samples=exp_attributes["num_samples"],
         seed=exp_attributes["seed"],
     )
     gate_error_ratio = {
         ((0,), "id"): 1,
         ((0,), "rz"): 0,
         ((0,), "sx"): 1,
         ((0,), "x"): 1,
         ((0, 1), "cx"): 1,
     }
     rb_exp.set_analysis_options(gate_error_ratio=gate_error_ratio)
     analysis_results = rb_exp.run_analysis(expdata1)
     return data, analysis_results
예제 #3
0
 def test_experiment_config(self):
     """Test converting to and from config works"""
     exp = InterleavedRB(CXGate(), [0, 1],
                         lengths=[10, 20, 30, 40],
                         num_samples=10)
     loaded_exp = InterleavedRB.from_config(exp.config())
     self.assertNotEqual(exp, loaded_exp)
     self.assertTrue(self.json_equiv(exp, loaded_exp))
def _generate_int_rb_fitter_data(dir_name: str, rb_exp_name: str,
                                 exp_attributes: dict):
    """
    Executing standard RB experiment and storing its data in json format.
    The json is composed of a list that the first element is a dictionary containing
    the experiment attributes and the second element is a list with all the experiment
    data.
    Args:
        dir_name: The json file name that the program write the data to.
        rb_exp_name: The experiment name for naming the output files.
        exp_attributes: attributes to config the RB experiment.
    """
    gate_error_ratio = {
        ((0, ), "id"): 1,
        ((0, ), "rz"): 0,
        ((0, ), "sx"): 1,
        ((0, ), "x"): 1,
        ((0, 1), "cx"): 1,
    }
    interleaved_gates = {"x": XGate(), "cx": CXGate()}
    transpiled_base_gate = ["cx", "sx", "x"]
    results_file_path = os.path.join(dir_name,
                                     str(rb_exp_name + "_output_data.json"))
    analysis_file_path = os.path.join(
        dir_name, str(rb_exp_name + "_output_analysis.json"))
    noise_model = create_depolarizing_noise_model()
    backend = AerSimulator(seed_simulator=exp_attributes["seed"])
    print("Generating experiment")
    rb_exp = InterleavedRB(
        interleaved_gates[exp_attributes["interleaved_element"]],
        exp_attributes["physical_qubits"],
        exp_attributes["lengths"],
        num_samples=exp_attributes["num_samples"],
        seed=exp_attributes["seed"],
    )
    rb_exp.analysis.set_options(gate_error_ratio=gate_error_ratio)
    print("Running experiment")
    experiment_obj = rb_exp.run(backend,
                                noise_model=noise_model,
                                basis_gates=transpiled_base_gate)
    experiment_obj.block_for_results()
    print("Done running experiment")
    exp_results = experiment_obj.data()
    with open(results_file_path, "w") as json_file:
        joined_list_data = [exp_attributes, exp_results]
        json_file.write(json.dumps(joined_list_data))
    _analysis_save(experiment_obj.analysis_results(), analysis_file_path)
예제 #5
0
    def test_interleaved_structure(self, interleaved_element: "Gate",
                                   qubits: list, length: int):
        """
        Verifies that when generating an interleaved circuit, it will be
        identical to the original circuit up to additions of
        barrier and interleaved element between any two cliffords
        Args:
            interleaved_element: The clifford element to interleave
            qubits: A list containing qubit indices for the experiment
            length: The number of cliffords in the tested circuit
        """
        exp = InterleavedRB(qubits=qubits,
                            interleaved_element=interleaved_element,
                            lengths=[length],
                            num_samples=1)

        circuits = exp.circuits()
        c_std = circuits[0]
        c_int = circuits[1]
        if c_std.metadata["interleaved"]:
            c_std, c_int = c_int, c_std
        num_cliffords = c_std.metadata["xval"]
        std_idx = 0
        int_idx = 0
        for _ in range(num_cliffords):
            # barrier
            self.assertEqual(c_std[std_idx][0].name, "barrier")
            self.assertEqual(c_int[int_idx][0].name, "barrier")
            # clifford
            self.assertEqual(c_std[std_idx + 1], c_int[int_idx + 1])
            # for interleaved circuit: barrier + interleaved element
            self.assertEqual(c_int[int_idx + 2][0].name, "barrier")
            self.assertEqual(c_int[int_idx + 3][0].name,
                             interleaved_element.name)
            std_idx += 2
            int_idx += 4
예제 #6
0
 def test_roundtrip_serializable(self):
     """Test round trip JSON serialization"""
     exp = InterleavedRB(CXGate(), [0, 1],
                         lengths=[10, 20, 30, 40],
                         num_samples=10)
     self.assertRoundTripSerializable(exp, self.json_equiv)