Ejemplo n.º 1
0
    def test_full_qpt(self, num_qubits, fitter):
        """Test QPT experiment"""
        backend = AerSimulator(seed_simulator=9000)
        seed = 1234
        f_threshold = 0.94
        target = qi.random_unitary(2**num_qubits, seed=seed)
        qstexp = ProcessTomography(target)
        if fitter:
            qstexp.analysis.set_options(fitter=fitter)
        expdata = qstexp.run(backend)
        results = expdata.analysis_results()

        # Check state is density matrix
        state = filter_results(results, "state").value
        self.assertTrue(isinstance(state, qi.Choi),
                        msg="fitted state is not a Choi matrix")

        # Check fit state fidelity
        fid = filter_results(results, "process_fidelity").value
        self.assertGreater(fid, f_threshold, msg="fit fidelity is low")
        # Manually check fidelity
        target_fid = qi.process_fidelity(state,
                                         target,
                                         require_tp=False,
                                         require_cp=False)
        self.assertAlmostEqual(fid,
                               target_fid,
                               places=6,
                               msg="result fidelity is incorrect")
Ejemplo n.º 2
0
    def test_qpt_teleport(self):
        """Test subset state tomography generation"""
        # NOTE: This test breaks transpiler. I think it is a bug with
        # conditionals in Terra.

        # Teleport qubit 0 -> 2
        backend = AerSimulator(seed_simulator=9000)
        exp = ProcessTomography(teleport_circuit(),
                                measurement_qubits=[2],
                                preparation_qubits=[0])
        expdata = exp.run(backend, shots=10000)
        results = expdata.analysis_results()

        # Check result
        f_threshold = 0.95

        # Check state is density matrix
        state = filter_results(results, "state").value
        self.assertTrue(isinstance(state, qi.Choi),
                        msg="fitted state is not a Choi matrix")

        # Manually check fidelity
        fid = qi.process_fidelity(state, require_tp=False, require_cp=False)
        self.assertGreater(fid,
                           f_threshold,
                           msg="fitted state fidelity is low")
Ejemplo n.º 3
0
 def test_experiment_config(self):
     """Test converting to and from config works"""
     exp = ProcessTomography(teleport_circuit(),
                             measurement_qubits=[2],
                             preparation_qubits=[0])
     loaded_exp = ProcessTomography.from_config(exp.config())
     self.assertNotEqual(exp, loaded_exp)
     self.assertTrue(self.json_equiv(exp, loaded_exp))
Ejemplo n.º 4
0
    def test_mixed_batch_exp(self):
        """Test batch state and process tomography experiment"""
        # Subsystem unitaries
        state_op = qi.random_unitary(2, seed=321)
        chan_op = qi.random_unitary(2, seed=123)

        state_target = qi.Statevector(state_op.to_instruction())
        chan_target = qi.Choi(chan_op.to_instruction())

        state_exp = StateTomography(state_op)
        chan_exp = ProcessTomography(chan_op)
        batch_exp = BatchExperiment([state_exp, chan_exp])

        # Run batch experiments
        backend = AerSimulator(seed_simulator=9000)
        par_data = batch_exp.run(backend)
        self.assertExperimentDone(par_data)

        f_threshold = 0.95

        # Check state tomo results
        state_results = par_data.child_data(0).analysis_results()
        state = filter_results(state_results, "state").value

        # Check fit state fidelity
        state_fid = filter_results(state_results, "state_fidelity").value
        self.assertGreater(state_fid, f_threshold, msg="fit fidelity is low")

        # Manually check fidelity
        target_fid = qi.state_fidelity(state, state_target, validate=False)
        self.assertAlmostEqual(state_fid,
                               target_fid,
                               places=6,
                               msg="result fidelity is incorrect")

        # Check process tomo results
        chan_results = par_data.child_data(1).analysis_results()
        chan = filter_results(chan_results, "state").value

        # Check fit process fidelity
        chan_fid = filter_results(chan_results, "process_fidelity").value
        self.assertGreater(chan_fid, f_threshold, msg="fit fidelity is low")

        # Manually check fidelity
        target_fid = qi.process_fidelity(chan,
                                         chan_target,
                                         require_cp=False,
                                         require_tp=False)
        self.assertAlmostEqual(chan_fid,
                               target_fid,
                               places=6,
                               msg="result fidelity is incorrect")
    def test_batch_exp_with_measurement_qubits(self):
        """Test batch process tomography experiment with kwargs"""
        seed = 1111
        nq = 3
        ops = [qi.random_unitary(2, seed=seed + i) for i in range(nq)]

        # Preparation circuit
        circuit = QuantumCircuit(nq)
        for i, op in enumerate(ops):
            circuit.append(op, [i])

        # Component experiments
        exps = []
        targets = []
        for i in range(nq):
            targets.append(ops[i])
            exps.append(
                ProcessTomography(circuit,
                                  measurement_qubits=[i],
                                  preparation_qubits=[i]))

        # Run batch experiments
        backend = AerSimulator(seed_simulator=9000)
        batch_exp = BatchExperiment(exps)
        batch_data = batch_exp.run(backend)
        batch_data.block_for_results()

        # Check target fidelity of component experiments
        f_threshold = 0.95
        for i in range(batch_exp.num_experiments):
            results = batch_data.component_experiment_data(
                i).analysis_results()

            # Check state is density matrix
            state = filter_results(results, "state").value
            self.assertTrue(isinstance(state, qi.Choi),
                            msg="fitted state is not a Choi matrix")

            # Check fit state fidelity
            fid = filter_results(results, "process_fidelity").value
            self.assertGreater(fid, f_threshold, msg="fit fidelity is low")

            # Manually check fidelity
            target_fid = qi.process_fidelity(state,
                                             targets[i],
                                             require_tp=False,
                                             require_cp=False)
            self.assertAlmostEqual(fid,
                                   target_fid,
                                   places=6,
                                   msg="result fidelity is incorrect")
Ejemplo n.º 6
0
    def test_full_exp_meas_prep_qubits(self, qubits):
        """Test subset state tomography generation"""
        # Subsystem unitaries
        seed = 1111
        nq = 3
        ops = [qi.random_unitary(2, seed=seed + i) for i in range(nq)]

        # Target state
        target_circ = QuantumCircuit(len(qubits))
        for i, qubit in enumerate(qubits):
            target_circ.append(ops[qubit], [i])
        target = qi.Operator(target_circ)

        # Preparation circuit
        circ = QuantumCircuit(nq)
        for i, op in enumerate(ops):
            circ.append(op, [i])

        # Run
        backend = AerSimulator(seed_simulator=9000)
        exp = ProcessTomography(circ,
                                measurement_qubits=qubits,
                                preparation_qubits=qubits)
        expdata = exp.run(backend)
        self.assertExperimentDone(expdata)
        results = expdata.analysis_results()

        # Check result
        f_threshold = 0.95

        # Check state is density matrix
        state = filter_results(results, "state").value
        self.assertTrue(isinstance(state, qi.Choi),
                        msg="fitted state is not a Choi matrix")

        # Check fit state fidelity
        fid = filter_results(results, "process_fidelity").value
        self.assertGreater(fid, f_threshold, msg="fit fidelity is low")

        # Manually check fidelity
        target_fid = qi.process_fidelity(state,
                                         target,
                                         require_tp=False,
                                         require_cp=False)
        self.assertAlmostEqual(fid,
                               target_fid,
                               places=6,
                               msg="result fidelity is incorrect")
Ejemplo n.º 7
0
    def test_parallel_exp(self):
        """Test parallel process tomography experiment"""
        # Subsystem unitaries
        seed = 1221
        nq = 4
        ops = [qi.random_unitary(2, seed=seed + i) for i in range(nq)]

        # Component experiments
        exps = []
        targets = []
        for i in range(nq):
            exps.append(ProcessTomography(ops[i], qubits=[i]))
            targets.append(ops[i])

        # Run batch experiments
        backend = AerSimulator(seed_simulator=9000)
        par_exp = ParallelExperiment(exps)
        par_data = par_exp.run(backend)
        self.assertExperimentDone(par_data)

        # Check target fidelity of component experiments
        f_threshold = 0.95
        for i in range(par_exp.num_experiments):
            results = par_data.child_data(i).analysis_results()

            # Check state is density matrix
            state = filter_results(results, "state").value
            self.assertTrue(isinstance(state, qi.Choi),
                            msg="fitted state is not a Choi matrix")

            # Check fit state fidelity
            fid = filter_results(results, "process_fidelity").value
            self.assertGreater(fid, f_threshold, msg="fit fidelity is low")

            # Manually check fidelity
            target_fid = qi.process_fidelity(state,
                                             targets[i],
                                             require_tp=False,
                                             require_cp=False)
            self.assertAlmostEqual(fid,
                                   target_fid,
                                   places=6,
                                   msg="result fidelity is incorrect")
Ejemplo n.º 8
0
    def test_exp_measurement_preparation_qubits(self, qubits):
        """Test subset measurement process tomography generation"""
        # Subsystem unitaries
        seed = 1111
        nq = 3
        ops = [qi.random_unitary(2, seed=seed + i) for i in range(nq)]

        # Preparation circuit
        circ = QuantumCircuit(nq)
        for i, op in enumerate(ops):
            circ.append(op, [i])

        num_meas = len(qubits)
        exp = ProcessTomography(circ,
                                measurement_qubits=qubits,
                                preparation_qubits=qubits)
        tomo_circuits = exp.circuits()

        # Check correct number of circuits are generated
        size = 3**num_meas * 4**num_meas
        self.assertEqual(len(tomo_circuits), size)

        # Check circuit metadata is correct
        for circ in tomo_circuits:
            meta = circ.metadata
            clbits = meta.get("clbits")
            self.assertEqual(clbits,
                             list(range(num_meas)),
                             msg="metadata clbits is incorrect")

        # Check experiment target metadata is correct
        exp_meta = exp._metadata()
        target_state = exp_meta.get("target")

        target_circ = QuantumCircuit(num_meas)
        for i, qubit in enumerate(qubits):
            target_circ.append(ops[qubit], [i])
        fid = qi.process_fidelity(target_state, qi.Operator(target_circ))
        self.assertGreater(fid, 0.99, msg="target_state is incorrect")