コード例 #1
0
import pyxacc as xacc
from pyxacc import qpu

xacc.Initialize()


# Create the source code
@qpu(accelerator='ibm')
def teleport():
    X(0)
    H(1)
    CNOT(1, 2)
    CNOT(0, 1)
    CNOT(1, 2)
    CNOT(2, 0)
    Measure(2, 0)
    return


results = teleport()

# Display the results
print(results.getMeasurementStrings())

# Finalize the framework
xacc.Finalize()
コード例 #2
0
ファイル: h2_exp_vals.py プロジェクト: czhao39/xacc-projectq
def main(argv=None):

   # Store results to this CSV file
   file = open('projectq_out.csv', 'w')
   file.write('theta, Z0_pq, Z0_tnqvm\n') #, Z1, Z0Z1\n')

   # Initialize XACC
   xacc.Initialize()
   
   # Indicate that we want to use the ProjectQ QASM 
   # Compiler/Transpiler
   xacc.setOption('compiler', 'projectq-qasm')
   
   # Get reference to the TNQVM Accelerator
   tnqvm = xacc.getAccelerator('tnqvm')
   
   # Allocate and AcceleratorBuffer
   xacc_qbits = tnqvm.createBuffer('qreg', 2)

   # Loop over H2 state prep variational parameters
   for theta in np.linspace(-np.pi, np.pi, 100):

      # Create a ProjectQ Engine with our CommandPrinter
      output = StringIO.StringIO()
      eng = MainEngine(Simulator(), [XaccCommandPrinter(output)])

      # Allocate some ProjectQ qubits
      qreg = eng.allocate_qureg(2)

      # Run Init State Circuit and Generate the Z0 QubitOperator
      op = Z0Term(qreg, theta)
      eng.flush()
      
      # Get Expectation Value of Z0 operator
      e_pq = eng.backend.get_expectation_value(op, qreg)
      Measure | qreg[0]

      # Get the ProjectQ QASM
      qasm = output.getvalue()
      
      # Generate an XACC Kernel
      xaccKernel = getXACCKernel(qasm, tnqvm)
   
      # Execute, no params since theta has
      # already been input to Z0Term function
      xaccKernel.execute(xacc_qbits, [])
      
      # Get the expectation value
      e_tnqvm = xacc_qbits.getExpectationValueZ()
      
      # Reset the qubits for the next iteration
      xacc_qbits.resetBuffer()
    
      # Store the results to a CSV file
      file.write(str(theta) + ', ' + str(e_pq) + ', ' + str(e_tnqvm) + '\n')
      file.flush()
   
   file.close()

   # Finalize the framework
   xacc.Finalize()