def test_setting_no_in(): out_ops = _generate_random_paulis(n_qubits=4, n_terms=7) for oop in out_ops: expt = ExperimentSetting(zeros_state(oop.get_qubits()), oop) expt2 = ExperimentSetting.from_str(str(expt)) assert expt == expt2 assert expt2.in_operator == functools.reduce(mul, [sZ(q) for q in oop.get_qubits()], sI()) assert expt2.out_operator == oop
def test_expt_settings_share_ntpb(): expts = [[ ExperimentSetting(zeros_state([0, 1]), sX(0) * sI(1)), ExperimentSetting(zeros_state([0, 1]), sI(0) * sX(1)) ], [ ExperimentSetting(zeros_state([0, 1]), sZ(0) * sI(1)), ExperimentSetting(zeros_state([0, 1]), sI(0) * sZ(1)) ]] for group in expts: for e1, e2 in itertools.combinations(group, 2): assert _max_weight_state([e1.in_state, e2.in_state]) is not None assert _max_weight_operator([e1.out_operator, e2.out_operator]) is not None
def test_R_operator_fixed_point_2_qubit(): # Check fixed point of operator. See Eq. 5 in Řeháček et al., PRA 75, 042108 (2007). qubits = [0, 1] id_setting = ExperimentSetting(in_state=zeros_state(qubits), out_operator=sI(qubits[0]) * sI(qubits[1])) zz_setting = ExperimentSetting(in_state=zeros_state(qubits), out_operator=sZ(qubits[0]) * sI(qubits[1])) id_result = ExperimentResult(setting=id_setting, expectation=1, total_counts=1) zzplus_result = ExperimentResult(setting=zz_setting, expectation=1, total_counts=1) zz_results = [id_result, zzplus_result] # Z basis test r = _R(P00, zz_results, qubits) actual = r @ P00 @ r np.testing.assert_allclose(actual, P00, atol=1e-12)
def _state_tomo_settings(qubits: Sequence[int]): """Yield settings over itertools.product(I, X, Y, Z). Used as a helper function for generate_state_tomography_experiment :param qubits: The qubits to tomographize. """ n_qubits = len(qubits) for o_ops in itertools.product([sI, sX, sY, sZ], repeat=n_qubits): o_op = functools.reduce(mul, (op(q) for op, q in zip(o_ops, qubits)), sI()) yield ExperimentSetting( in_state=zeros_state(qubits), out_operator=o_op, )
def test_for_negative_probabilities(): # trivial program to do state tomography on prog = Program(I(0)) # make TomographyExperiment expt_settings = [ExperimentSetting(zeros_state([0]), pt) for pt in [sI(0), sX(0), sY(0), sZ(0)]] experiment_1q = TomographyExperiment(settings=expt_settings, program=prog) # make an abstract compiler class DummyCompiler(AbstractCompiler): def get_version_info(self): return {} def quil_to_native_quil(self, program: Program): return program def native_quil_to_executable(self, nq_program: Program): return nq_program # make a quantum computer object device = NxDevice(nx.complete_graph(1)) qc_density = QuantumComputer(name='testy!', qam=PyQVM(n_qubits=1, quantum_simulator_type=ReferenceDensitySimulator), device=device, compiler=DummyCompiler()) # initialize with a pure state initial_density = np.array([[1.0, 0.0], [0.0, 0.0]]) qc_density.qam.wf_simulator.density = initial_density try: list(measure_observables(qc=qc_density, tomo_experiment=experiment_1q, n_shots=3000)) except ValueError as e: # the error is from np.random.choice by way of self.rs.choice in ReferenceDensitySimulator assert str(e) != 'probabilities are not non-negative' # initialize with a mixed state initial_density = np.array([[0.9, 0.0], [0.0, 0.1]]) qc_density.qam.wf_simulator.density = initial_density try: list(measure_observables(qc=qc_density, tomo_experiment=experiment_1q, n_shots=3000)) except ValueError as e: assert str(e) != 'probabilities are not non-negative'
from pyquil.paulis import sI, sZ, sX from pyquil.quil import Program from rpcq.messages import PyQuilExecutableResponse from forest.benchmarking import distance_measures as dm # Single qubit defs Q0 = 0 PROJ_ZERO = np.array([[1, 0], [0, 0]]) PROJ_ONE = np.array([[0, 0], [0, 1]]) ID = PROJ_ZERO + PROJ_ONE PLUS = np.array([[1], [1]]) / np.sqrt(2) PROJ_PLUS = PLUS @ PLUS.T.conj() PROJ_MINUS = ID - PROJ_PLUS ID_SETTING = ExperimentSetting(in_state=zeros_state([Q0]), out_operator=sI(Q0)) Z_SETTING = ExperimentSetting(in_state=zeros_state([Q0]), out_operator=sZ(Q0)) X_SETTING = ExperimentSetting(in_state=zeros_state([Q0]), out_operator=sX(Q0)) # Two qubit defs P00 = np.kron(PROJ_ZERO, PROJ_ZERO) P01 = np.kron(PROJ_ZERO, PROJ_ONE) P10 = np.kron(PROJ_ONE, PROJ_ZERO) P11 = np.kron(PROJ_ONE, PROJ_ONE) def test_generate_1q_state_tomography_experiment(): qubits = [0] prog = Program(I(qubits[0])) one_q_exp = generate_state_tomography_experiment(prog, qubits=qubits) dimension = 2**len(qubits)