Example #1
0
    def execute(self, inputParams):
        xacc_opts = inputParams['XACC']
        acc_name = xacc_opts['accelerator']

        if 'verbose' in xacc_opts and xacc_opts['verbose']:
            xacc.set_verbose(True)

        if 'Benchmark' not in inputParams:
            xacc.error(
                'Invalid benchmark input - must have Benchmark description')

        if 'Observable' not in inputParams:
            xacc.error(
                'Invalid benchmark input - must have Observable description')

        if 'Ansatz' not in inputParams:
            xacc.error(
                'Invalid benchmark input - must have Ansatz circuit description'
            )

        H = None
        if inputParams['Observable']['name'] == 'pauli':
            obs_str = inputParams['Observable']['obs_str']
            H = xacc.getObservable('pauli', obs_str)
        elif inputParams['Observable']['name'] == 'fermion':
            obs_str = inputParams['Observable']['obs_str']
            H = xacc.getObservable('fermion', obs_str)
        elif inputParams['Observable']['name'] in ['pyscf', 'psi4']:
            opts = {
                'basis': inputParams['Observable']['basis'],
                'geometry': inputParams['Observable']['geometry']
            }
            if 'fo' in inputParams['Observable'] and 'ao' in inputParams[
                    'Observable']:
                opts['frozen-spin-orbitals'] = ast.literal_eval(
                    inputParams['Observable']['fo'])
                opts['active-spin-orbitals'] = ast.literal_eval(
                    inputParams['Observable']['ao'])
            H = xacc.getObservable(inputParams['Observable']['name'], opts)

        #print('Ham: ', H.toString())
        qpu = xacc.getAccelerator(acc_name, xacc_opts)
        if 'Decorators' in inputParams:
            if 'readout_error' in inputParams['Decorators']:
                qpu = xacc.getAcceleratorDecorator('ro-error', qpu)

        buffer = xacc.qalloc(H.nBits())
        optimizer = None
        if 'Optimizer' in inputParams:
            # check that values that can be ints/floats are
            opts = inputParams['Optimizer']
            for k, v in inputParams['Optimizer'].items():
                try:
                    i = int(v)
                    opts[k] = i
                    continue
                except:
                    pass
                try:
                    f = float(v)
                    opts[k] = f
                    continue
                except:
                    pass
            optimizer = xacc.getOptimizer(
                inputParams['Optimizer']['name']
                if 'Optimizer' in inputParams else 'nlopt', opts)
        else:
            optimizer = xacc.getOptimizer('nlopt')

        provider = xacc.getIRProvider('quantum')

        if inputParams['Benchmark']['algorithm'] == 'adapt-vqe':
            alg = xacc.getAlgorithm(
                inputParams['Benchmark']['algorithm'], {
                    'pool': inputParams['Ansatz']['pool'],
                    'nElectrons': int(inputParams['Ansatz']['electrons']),
                    'accelerator': qpu,
                    'observable': H,
                    'optimizer': optimizer,
                })

            alg.execute(buffer)
            return buffer

        else:

            if 'source' in inputParams['Ansatz']:
                # here assume this is xasm always
                src = inputParams['Ansatz']['source']
                xacc.qasm(src)
                # get the name of the circuit
                circuit_name = None
                for l in src.split('\n'):
                    if '.circuit' in l:
                        circuit_name = l.split(' ')[1]
                ansatz = xacc.getCompiled(circuit_name)
            else:
                ansatz = provider.createInstruction(
                    inputParams['Ansatz']['ansatz'])
                ansatz = xacc.asComposite(ansatz)

            alg = xacc.getAlgorithm(
                inputParams['Benchmark']['algorithm'], {
                    'ansatz': ansatz,
                    'accelerator': qpu,
                    'observable': H,
                    'optimizer': optimizer,
                })

            alg.execute(buffer)
            return buffer
Example #2
0
import xacc

qpu = xacc.getAccelerator(
    'ibm', {'shots': 256, 'backend': 'lowest-queue-count', 'n-qubits': 5, 'check-jobs-limit': True})

print(qpu.getProperties()["total-json"])

xacc.qasm('''.compiler xasm
.circuit bell
.qbit q
H(q[0]);
CX(q[0],q[1]);
Measure(q[0]);
Measure(q[1]);
''')
bell = xacc.getCompiled('bell')

q = xacc.qalloc(2)
qpu.execute(q, bell)

print(q)
import matplotlib.pyplot as plt

# Get access to the desired QPU and
# allocate some qubits to run on
qpu = xacc.getAccelerator('qpp')

# Construct the H2 Hamiltonian
ham = xacc.getObservable(
    'pauli', '5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1')

xacc.qasm('''.compiler xasm
.circuit prep
.qbit q
X(q[0]);
''')
prep_circuit = xacc.getCompiled('prep')

# We need 2 qubits for this case (H2)
buffer = xacc.qalloc(2)

# Horizontal axis: 0 -> 0.3 (step = 0.05)
# The number of Trotter steps
nbSteps = 6

# The Trotter step size
stepSize = 0.05

# Create the QITE algorithm
qite = xacc.getAlgorithm(
    'qite', {
        'accelerator': qpu,
Example #4
0
print('Energy = ', buffer.getInformation('opt-val'))
print('Opt Angles = ', buffer.getInformation('opt-params'))


# Define the ansatz and decorate it to indicate
# you'd like to run VQE
xacc.qasm('''.compiler xasm
.circuit ansatz2
.parameters t0
.qbit q
X(q[0]);
Ry(q[1],t0);
CX(q[1],q[0]);
''')
ansatz2 = xacc.getCompiled('ansatz2')
opt = xacc.getOptimizer('nlopt')

# Create the VQE algorithm
vqe = xacc.getAlgorithm('vqe', {
                        'ansatz': ansatz2,
                        'accelerator': qpu,
                        'observable': ham,
                        'optimizer': opt
                        })
vqe.execute(buffer)

# Now lets just do a param sweep
ansatz.modifyAlgorithm('energy')

# Execute, starting at .5
Example #5
0
qbits = xacc.qalloc(1)

# Get the MLPack Optimizer, default is Adam
optimizer = xacc.getOptimizer('mlpack')

# Create a simple quantum program
xacc.qasm('''
.compiler xasm
.circuit foo
.parameters x,y,z
.qbit q
Ry(q[0], x);
Ry(q[0], y);
Ry(q[0], z);
''')
f = xacc.getCompiled('foo')

f.defaultPlacement(qpu, {'qubit-map': [0]})

print(f.toString())
# exit(0)
# Get the DDCL Algorithm, initialize it
# with necessary parameters
ddcl = xacc.getAlgorithm(
    'ddcl', {
        'ansatz': f,
        'accelerator': qpu,
        'target_dist': [.5, .5],
        'optimizer': optimizer,
        'loss': 'js',
        'gradient': 'js-parameter-shift'
Example #6
0
.circuit teleport
.qbit q
X(q[0]);
// Bell channel setup
H(q[1]);
CX(q[1], q[2]);
// Alice Bell measurement
CX(q[0], q[1]);
H(q[0]);
Measure(q[0]);
Measure(q[1]);
// Correction
if (q[0])
{
    Z(q[2]);
}
if (q[1])
{
    X(q[2]);
}
// Measure teleported qubit
Measure(q[2]);
''')

teleport = xacc.getCompiled('teleport')

q = xacc.qalloc(3)
qq = xacc.qalloc()
qpu.execute(q, teleport)

print(q)