Browse Source

Begin implementation of the circuit representation

Adam Kelly 5 years ago
parent
commit
9b1b80c046
2 changed files with 84 additions and 0 deletions
  1. 56 0
      python/circuits/__init__.py
  2. 28 0
      python/circuits/instruction.py

+ 56 - 0
python/circuits/__init__.py

@@ -0,0 +1,56 @@
+"""
+This file contains the main circuit implementation/representation class
+"""
+
+
+class Instruction:
+    def __init__(self, name, qubits, parameters=None):
+        self.name = name
+        self.qubits = qubits
+        self.parameters = parameters
+
+    def __repr__(self):
+        if self.parameters:
+            return "{} ({}) {};".format(self.name, self.parameters, self.qubits)
+        return "{} {};".format(self.name, self.qubits)
+
+
+class Circuit:
+    """
+    Represents a Quantum Circuit. 
+
+    The number of qubits is inferred from 
+    the instructions provided
+    """
+
+    def __init__(self):
+        self.num_qubits = 0
+
+        # Represents the instructions that are given to
+        # the quantum circuit
+        self.instructions = []
+
+        # Represent the numerical remapping in the circuit,
+        # to avoid badly labelled registers.
+        self.idx_map = {}
+
+    # TODO:
+    # - Mirror
+    # - Invert
+    # - Combine multiple circuits
+    # - Multiple register handling
+
+    def add_instruction(self, gate_name, *qubits):
+        # instruction
+        pass
+
+    def __repr__(self):
+        return str(vars(self))
+
+
+if __name__ == "__main__":
+    circuit = Circuit(3)
+    print(circuit)
+
+    h = Instruction('h', [0])
+    print(h)

+ 28 - 0
python/circuits/instruction.py

@@ -0,0 +1,28 @@
+"""
+The class defined here describes all
+possible instructions that can occur.
+This includes:
+
+- Adding quantum gates
+- Measurement (including sampling, etc.)
+- Reset
+- Initialization
+
+The instructions defined here are independent
+of backend, and each of the backends should
+implement their own validation step, which
+should also correspond to the backend selection
+procedure.
+"""
+
+
+# An instruction consists of a number of parts
+
+class Instruction:
+    def __init__(self, name, num_qubits, num):
+        pass
+
+
+class Gate(Instruction):
+    def __init__(self):
+        pass