# A Graph-Based Approach for Modelling Quantum Circuits
## 4 Modelling the Unitary Circuit Model of Quantum Computation
### Completing the Metamodel with OCL Constraints  
Metamodels are usually completed by adding OCL [30] constraints, which define a set of rules that every model must satisfy in order to be well-formed. Besides the usual constraints for DAGs (i.e., no Edge to self is allowed, no Node can remain without at least one incoming/outgoing Edge, no two Edges can have the same source and destination Node), we define the following specific constraints, shown in Listing 1, tailored to the particularities of quantum circuits. It is worth highlighting that the constraints listed below are independent of the modelling strategy used in the model:  
* All QuantumGates contained in a Node should operate on different Qubits. The OCL constraint GatesOperateOnDifferentQubits checks this.  
Figure 4: Graph representations that follow the five strategies for modelling the quantum circuit that solves the example riddle. Dashed lines represent the relationship between a control gate and the gate being controlled. They are not part of the metamodel but rather a visual aid added to better understand the graph representations.  
* The Qubit on which a Control gate operates must be different from the Qubit on which the controlled QuantumGate operates. Checked by OperatesOnADifferentQubitThanControlledGate OCL constraint.
* A Control gate cannot control another Control gate. Multi-controlled gates, such as the Toffoli gate, can be modelled by having each individual Control gate point to the gate it controls, instead of pointing to the next Control gate in the chain. The main drawback of this last modelling choice is that you could create models where Control gates just control other Control gates, potentially creating loops which will make it even harder to detect. The OCL constraint CannotControlAnotherControlGate checks it.
* If a Node contains more than one gate, then all the Control gates within it must control gates also contained on that node. From the point of view of model well-formedness, this constraint is not necessary, but has been added to ensure some logical order in the structure. The OCL constraint AllControlSameNode checks it.  
```
1 contextNodeinvGatesOperateOnDifferentQubits:
2 self.gates->forAll(g1,g2|g1<g2 impliesg1.qubit<g2.qubit)
3
4 contextControlinvOperatesOnADifferentQubitThanControlledGate:
5 self.qubit<self.controlGate.qubit
6
7 contextControlinvCannotControlAnotherControlGate:
8 mostself.controlGate.isTypeOf(Control)
9
10 contextNodeinvAllControlSameNode:
11 if(self.gates.size()>1)thenself.gates->
12 select(g|g.isTypeOf(Control))->
13 forAll(g|self.gates->includes(g.controlGate))
14 elsetrue
15 endif
16
17 contextCircuitinvEdges_QubitsContainGates:
18 notself.edges.isEmpty()andself.qubits->
19 exists(q|notq.gates.isEmpty())
20
21 contextCircuitinvNotAllColectionsEmpty:
22 notself.edges.isEmpty()ornotself.nodes.isEmpty()or
23 self.qubits->exists(q|notq.gates.isEmpty())
```  
**Listing 1**. OCL constraints to check model validity.  
The following constraints appear due to the addition of the containment reference mentioned before and shown in red in the metamodel. Their purpose will be clarified in Section 5.3:  
* A model that has Edges and in which at least one Qubit contains gates is not valid. The OCL constraint Edges_QubitsContainGates checks it.
* At least one of the collections Circuit.edges, Circuit.nodes or Qubit.gates must contain elements. This means that not all of these collections can be empty. The OCL constraint NotAllCollectionsEmpty checks it.  
At this point, we have provided a common metamodel for modelling quantum circuits, five strategies to model circuits according to this metamodel, the rules of construction of quantum circuits, and finally, a set of OCL constraints.