quantum-algorithms

git clone git://git.codymlewis.com/quantum-algorithms.git
Log | Files | Refs | README

entanglement (1316B)


      1 #!/usr/bin/env python3
      2 
      3 '''
      4 A simple quantum circuit that performs entanglement
      5 '''
      6 
      7 import argparse
      8 
      9 from qiskit import QuantumCircuit, execute
     10 from qiskit.providers.aer import Aer
     11 
     12 
     13 def to_statevector(a):
     14     '''Convert a binary string into its representative qubit statevector'''
     15     if a == "0":
     16         return [1, 0]
     17     return [0, 1]
     18 
     19 
     20 def create_circuit(in_states):
     21     '''Construct the circuit with the input states set'''
     22     n = len(in_states)
     23     qcircuit = QuantumCircuit(n)
     24     for i in range(n):
     25         qcircuit.initialize(to_statevector(in_states[i]), i)
     26     qcircuit.h(0)
     27     qcircuit.cnot(0, 1)
     28     meas = QuantumCircuit(n, n)
     29     meas.barrier(range(n))
     30     meas.measure(range(n), range(n))
     31     return qcircuit + meas
     32 
     33 
     34 if __name__ == '__main__':
     35     PARSER = argparse.ArgumentParser(
     36         description="A simple quantum circuit that performs entanglement"
     37     )
     38     PARSER.add_argument(
     39         'in_states', metavar='INPUT_STATES', type=str,
     40         help='Binary string representing the input states of the qubits'
     41     )
     42     ARGS = PARSER.parse_args()
     43     QCIRC = create_circuit(ARGS.in_states)
     44     print("Created circuit:")
     45     print(QCIRC.draw())
     46     BACKEND_SIM = Aer.get_backend('qasm_simulator')
     47     RESULT = execute(QCIRC, BACKEND_SIM).result()
     48     print(f'Result: {RESULT.get_counts()}')