custom
code
sovereign-compute
File size: 6,415 Bytes
e92f76f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// ============================================================
// QuantumVerifier.cpp — Type checking and linear-type enforcement
// ============================================================
// Enforces:
//   1. No-cloning: every !quantum.qubit has exactly one use
//   2. Angle domain: rational or symbolic, not arbitrary float
//   3. Bounds checking: extract_ref, subveq indices in range
//   4. Normalization: alloc_with_state vectors are normalized

#include "QuantumDialect.h"
#include "QuantumOps.h"
#include "QuantumTypes.h"

using namespace mlir;
using namespace mlir::quantum;

// ============================================================
// No-Cloning Verifier
// ============================================================

// Walk the use-def chain of every !quantum.qubit value and reject
// any SSA value that has >1 use (duplicate) or 0 uses (leak).

LogicalResult verifyNoCloning(Operation *op) {
  for (Value result : op->getResults()) {
    // Only check quantum types
    if (!isa<QubitType, QuregType>(result.getType()))
      continue;

    // Check for multiple uses (cloning)
    if (!result.hasOneUse()) {
      // Allow 0 uses only for function return values
      if (result.use_empty()) {
        if (auto funcOp = dyn_cast<func::FuncOp>(op->getParentOp())) {
          if (op == funcOp.getBody().front().getTerminator())
            continue;  // allowed at function return
        }
        return op->emitOpError()
               << "quantum resource has no use (leak detected)";
      }

      return op->emitOpError()
             << "quantum resource has " << result.getUses().size()
             << " uses (no-cloning violation: expected exactly 1)";
    }
  }
  return success();
}

// ============================================================
// UnitaryOp Verifier
// ============================================================

LogicalResult UnitaryOp::verify() {
  // 1. No-cloning
  if (failed(verifyNoCloning(getOperation())))
    return failure();

  // 2. Angle count matches qubit count for parameterized gates
  auto angles = getAngles();
  auto qubits = getQubits();
  if (qubits.size() != angles.size()) {
    // Allow single angle broadcast to all qubits
    if (angles.size() != 1)
      return emitOpError("angle count (")
             << angles.size() << ") must match qubit count ("
             << qubits.size() << ") or be a single broadcast angle";
  }

  // 3. Axis must be one of "X","Y","Z","arbitrary"
  if (auto axis = getAxis()) {
    StringRef a = axis.value();
    if (a != "X" && a != "Y" && a != "Z" && a != "arbitrary")
      return emitOpError("axis must be one of X, Y, Z, arbitrary; got '")
             << a << "'";
  }

  return success();
}

// ============================================================
// EntangleOp Verifier
// ============================================================

LogicalResult EntangleOp::verify() {
  // 1. No-cloning
  if (failed(verifyNoCloning(getOperation())))
    return failure();

  // 2. At least one control and one target
  if (getControls().empty())
    return emitOpError("entangle requires at least one control qubit");
  if (getTargets().empty())
    return emitOpError("entangle requires at least one target qubit");

  // 3. Output count matches input count
  if (getOutControls().size() != getControls().size())
    return emitOpError("output control count must match input control count");
  if (getOutTargets().size() != getTargets().size())
    return emitOpError("output target count must match input target count");

  return success();
}

// ============================================================
// MeasureOp Verifier
// ============================================================

LogicalResult MeasureOp::verify() {
  // 1. No-cloning
  if (failed(verifyNoCloning(getOperation())))
    return failure();

  // 2. Output bit count matches input qubit count
  if (getBits().size() != getQubits().size())
    return emitOpError("bit count must match qubit count");

  // 3. Collapsed count matches input qubit count
  if (getCollapsed().size() != getQubits().size())
    return emitOpError("collapsed count must match qubit count");

  return success();
}

// ============================================================
// AllocOp Verifier
// ============================================================

LogicalResult AllocOp::verify() {
  // No-cloning (should always pass for alloc)
  return verifyNoCloning(getOperation());
}

// ============================================================
// ExtractRefOp Verifier
// ============================================================

LogicalResult ExtractRefOp::verify() {
  // Bounds check
  if (auto sizeAttr = getSource().getType().dyn_cast<QuregType>().getSize()) {
    int64_t idx = getIndex().getSExtValue();
    if (idx < 0 || idx >= *sizeAttr)
      return emitOpError("index ")
             << idx << " out of bounds for qureg of size " << *sizeAttr;
  }
  return success();
}

// ============================================================
// SubveqOp Verifier
// ============================================================

LogicalResult SubveqOp::verify() {
  if (auto sizeAttr = getSource().getType().dyn_cast<QuregType>().getSize()) {
    int64_t low = getLow().getSExtValue();
    int64_t high = getHigh().getSExtValue();
    if (low < 0 || high > *sizeAttr || low >= high)
      return emitOpError("invalid range [")
             << low << ", " << high << ") for qureg of size " << *sizeAttr;
  }
  return success();
}

// ============================================================
// ExpPauliOp Verifier
// ============================================================

LogicalResult ExpPauliOp::verify() {
  // 1. No-cloning
  if (failed(verifyNoCloning(getOperation())))
    return failure();

  // 2. Pauli string length must match qubit count
  auto pauli = getPauli();
  auto qubits = getQubits();
  if (pauli.size() != qubits.size())
    return emitOpError("pauli string length (")
           << pauli.size() << ") must match qubit count ("
           << qubits.size() << ")";

  // 3. Pauli values must be 0-3 (I, X, Y, Z)
  for (auto [i, val] : llvm::enumerate(pauli)) {
    int p = val.cast<IntegerAttr>().getSExtValue();
    if (p < 0 || p > 3)
      return emitOpError("pauli[")
             << i << "] = " << p << " must be 0 (I), 1 (X), 2 (Y), or 3 (Z)";
  }

  return success();
}