-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOracles.qs
More file actions
81 lines (68 loc) · 2.92 KB
/
Copy pathOracles.qs
File metadata and controls
81 lines (68 loc) · 2.92 KB
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
namespace Quantum.Oracles {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Preparation;
/// # Operation: CheckPasswordOracle
/// Oráculo cuántico que marca una combinación de bits que representa una "contraseña" correcta.
/// Aquí simulamos que la contraseña correcta es un valor conocido (ej: "1011").
///
/// ## Entradas:
/// - `candidateKey`: qubits que representan la clave candidata (en binario).
/// - `target`: qubit auxiliar que se voltea si el candidato es correcto (efecto de fase si se usa con phase kickback).
///
/// ## Nota:
/// Este oráculo compara el estado cuántico con un valor clásico predefinido.
operation CheckPasswordOracle(
candidateKey: LittleEndian,
target: Qubit
) : Unit is Adj + Ctl {
let desiredKey = [true, false, true, true]; // Representa "1011" (clave correcta: 11 en decimal)
using (register = Qubit[Length(desiredKey)]) {
let regLE = LittleEndian(register);
// Convertir desiredKey (lista de bools) a estado cuántico |desiredKey⟩
ApplyZipWithCA(CNOT, desiredKey, register);
// Comparar: si candidateKey == desiredKey, aplicar X al target
within {
// Restar desiredKey de candidateKey → resultado cero si son iguales
SubtractI(regLE, candidateKey);
} apply {
// Si la diferencia es cero, candidateKey == desiredKey
X(target);
(Controlled OnFixedPoint(Zero))(candidateKey!, target);
X(target);
}
// Deshacer la resta
// (automático en `within...apply`)
}
}
/// # Operation: RunGroverOracle
/// Ejemplo de cómo usar el oráculo (para pruebas clásicas o simulación).
operation RunGroverOracle(keyBits: Bool[]) : Result {
// Convertir entrada clásica a qubits
using ((qubits, target) = (Qubit[Length(keyBits)], Qubit())) {
let candidate = LittleEndian(qubits);
// Preparar estado |keyBits⟩
for i in 0..Length(keyBits)-1 {
if keyBits[i] {
X(qubits[i]);
}
}
// Aplicar oráculo
CheckPasswordOracle(candidate, target);
// Medir si fue marcado
let result = MResetZ(target);
// Resetear qubits
ResetAll(qubits);
return result;
}
}
}
namespace Quantum.Oracles {
operation CheckPassword(InputHash : String) : Result {
// Aquí iría el código del oráculo que verifica si una clave coincide con el hash
Message($"🔎 Revisando hash: {InputHash}");
return Zero;
}
}