Ejemplo n.º 1
0
    def general_test(self, pauli, num_qubits=None, seed=None):
        """General test case"""
        pauli_qubits = list(range(len(pauli)))
        if num_qubits is None:
            num_qubits = len(pauli_qubits)

        # Prepare random N-qubit product input state
        # from seed
        rng = np.random.default_rng(seed)
        params = rng.uniform(-1, 1, size=(num_qubits, 3))
        init_circ = QuantumCircuit(num_qubits)
        for i, par in enumerate(params):
            init_circ.u3(*par, i)

        # Compute the target expectation value
        rho = DensityMatrix.from_instruction(init_circ)
        op = Operator.from_label(pauli)
        target = np.trace(Operator(rho).compose(op, pauli_qubits).data)

        # Simulate expectation value
        qc = init_circ.copy()
        qc.snapshot_expectation_value('final', [(1, pauli)], pauli_qubits)
        qobj = assemble(qc)
        result = self.SIMULATOR.run(
            qobj, backend_options=self.BACKEND_OPTS).result()
        self.assertSuccess(result)
        snapshots = result.data(0).get('snapshots', {})
        self.assertIn('expectation_value', snapshots)
        self.assertIn('final', snapshots['expectation_value'])
        expval = snapshots.get('expectation_value', {})['final'][0]['value']
        self.assertAlmostEqual(expval, target)
Ejemplo n.º 2
0
def test_qsphere_bad_dm_input():
    """Tests the qsphere raises when passed impure dm"""

    qc = QuantumCircuit(3)
    qc.h(0)
    qc.cx(0, 1)
    qc.cx(1, 2)
    dm = DensityMatrix.from_instruction(qc)
    pdm = partial_trace(dm, [0, 1])
    with pytest.raises(KaleidoscopeError):
        assert qsphere(pdm)
Ejemplo n.º 3
0
def sys_evolve_den(nsites, excitations, total_time, dt, hop, U, trotter_steps):
    #Check for correct data types of input
    if not isinstance(nsites, int):
        raise TypeError("Number of sites should be int")
    if np.isscalar(excitations):
        raise TypeError("Initial state should be list or numpy array")
    if not np.isscalar(total_time):
        raise TypeError("Evolution time should be scalar")
    if not np.isscalar(dt):
        raise TypeError("Time step should be scalar")
    if not isinstance(trotter_steps, int):
        raise TypeError("Number of trotter slices should be int")

    numq = 2 * nsites
    num_steps = int(total_time / dt)
    print('Num Steps: ', num_steps)
    print('Total Time: ', total_time)
    data = []

    for t_step in range(0, num_steps):
        #Create circuit with t_step number of steps
        q = QuantumRegister(numq)
        c = ClassicalRegister(numq)
        qcirc = QuantumCircuit(q, c)

        #=========USE THIS REGION TO SET YOUR INITIAL STATE==============
        #Loop over each excitation
        for flip in excitations:
            qcirc.x(flip)
            #qcirc.h(flip)
#            qcirc.z(flip)
#===============================================================

        qcirc.barrier()
        #Append circuit with Trotter steps needed
        qc_evolve(qcirc, nsites, t_step * dt, dt, hop, U, trotter_steps)
        den_mtrx_obj = DensityMatrix.from_instruction(qcirc)
        den_mtrx = den_mtrx_obj.to_operator().data
        state_vector = qi.Statevector.from_instruction(qcirc)
        #data.append(state_vector.data)
        data.append(den_mtrx)

        #Measure the circuit
        for i in range(numq):
            qcirc.measure(i, i)
        '''
    #Choose provider and backend
        provider = IBMQ.get_provider()
        #backend = Aer.get_backend('statevector_simulator')
        backend = Aer.get_backend('qasm_simulator')
        #backend = provider.get_backend('ibmq_qasm_simulator')
        #backend = provider.get_backend('ibmqx4')
        #backend = provider.get_backend('ibmqx2')
        #backend = provider.get_backend('ibmq_16_melbourne')

        shots = 8192
        max_credits = 10 #Max number of credits to spend on execution
        job_exp = execute(qcirc, backend=backend, shots=shots, max_credits=max_credits)
        job_monitor(job_exp)
        result = job_exp.result()
        counts = result.get_counts(qcirc)
        print(result.get_counts(qcirc))
        print("Job: ",t_step+1, " of ", num_steps," complete.")
        '''
    return data