Note
Go to the end to download the full example code.
HoloVec Quickstart Guide¶
Topics: Installation, basic workflow, encoding, binding, retrieval Time: 5 minutes Prerequisites: None Related: 01_basic_operations.py, 02_models_comparison.py
This quickstart demonstrates the core HoloVec workflow in under 100 lines. You’ll encode data, compose representations, and retrieve information using hyperdimensional computing - brain-inspired computing with 10,000-dimensional vectors.
16 from holovec import VSA
17
18 print("=" * 70)
19 print("HoloVec Quickstart - Hyperdimensional Computing in 5 Minutes")
20 print("=" * 70)
21 print()
22
23 # ============================================================================
24 # Step 1: Create a VSA Model
25 # ============================================================================
26 print("Step 1: Creating a VSA model")
27 print("-" * 70)
28
29 # Create a FHRR model (Fourier Holographic Reduced Representations)
30 # FHRR provides exact inverses and works well with continuous encoders
31 model = VSA.create('FHRR', dim=10000, seed=42)
32
33 print(f"Model: {model.model_name}")
34 print(f"Dimension: {model.dimension}")
35 print(f"Backend: {model.backend.name}")
36 print()
37
38 # ============================================================================
39 # Step 2: Encode Symbolic Data
40 # ============================================================================
41 print("Step 2: Encoding symbolic data")
42 print("-" * 70)
43
44 # Create random hypervectors for symbols
45 # Each symbol gets a unique 10,000-dimensional vector
46 alice = model.random(seed=1)
47 bob = model.random(seed=2)
48 loves = model.random(seed=3)
49 hates = model.random(seed=4)
50
51 print("Encoded symbols:")
52 print(f" alice → 10000-dim hypervector")
53 print(f" bob → 10000-dim hypervector")
54 print(f" loves → 10000-dim hypervector")
55 print(f" hates → 10000-dim hypervector")
56 print()
57
58 # ============================================================================
59 # Step 3: Compose Representations with Binding
60 # ============================================================================
61 print("Step 3: Composing structured representations")
62 print("-" * 70)
63
64 # Bind vectors to create structured representations
65 # Binding creates a new vector that is dissimilar to both operands
66 alice_loves_bob = model.bind(model.bind(alice, loves), bob)
67 alice_hates_bob = model.bind(model.bind(alice, hates), bob)
68
69 print("Created compositional structures:")
70 print(f" 'Alice loves Bob' → single hypervector")
71 print(f" 'Alice hates Bob' → single hypervector")
72 print()
73
74 # These two statements are completely different
75 similarity = model.similarity(alice_loves_bob, alice_hates_bob)
76 print(f"Similarity between statements: {similarity:.3f}")
77 print(" (Low similarity confirms they represent different facts)")
78 print()
79
80 # ============================================================================
81 # Step 4: Query and Retrieve Information
82 # ============================================================================
83 print("Step 4: Querying compositional structures")
84 print("-" * 70)
85
86 # Query: Who does Alice love?
87 # Unbind 'alice' and 'loves' from 'alice_loves_bob'
88 query_result = model.unbind(model.unbind(alice_loves_bob, alice), loves)
89
90 # Check similarity to answer
91 similarity_bob = model.similarity(query_result, bob)
92 similarity_alice = model.similarity(query_result, alice)
93
94 print("Query: Who does Alice love?")
95 print(f" Similarity to 'bob': {similarity_bob:.3f} ← Answer!")
96 print(f" Similarity to 'alice': {similarity_alice:.3f}")
97 print()
98
99 # ============================================================================
100 # Step 5: Encode Continuous Values
101 # ============================================================================
102 print("Step 5: Encoding continuous values")
103 print("-" * 70)
104
105 from holovec.encoders import FractionalPowerEncoder
106
107 # Create an encoder for temperatures (0-100°C)
108 temp_encoder = FractionalPowerEncoder(model, min_val=0, max_val=100, bandwidth=0.1)
109
110 # Encode temperatures
111 temp_25 = temp_encoder.encode(25.0)
112 temp_26 = temp_encoder.encode(26.0)
113 temp_50 = temp_encoder.encode(50.0)
114
115 print("Encoded temperatures:")
116 print(f" 25°C → hypervector")
117 print(f" 26°C → hypervector")
118 print(f" 50°C → hypervector")
119 print()
120
121 # Similar values have high similarity
122 sim_25_26 = model.similarity(temp_25, temp_26)
123 sim_25_50 = model.similarity(temp_25, temp_50)
124
125 print(f"Similarity (25°C, 26°C): {sim_25_26:.3f} ← Close values, high similarity")
126 print(f"Similarity (25°C, 50°C): {sim_25_50:.3f} ← Distant values, low similarity")
127 print()
128
129 # ============================================================================
130 # Step 6: Build Associative Memory
131 # ============================================================================
132 print("Step 6: Building associative memory")
133 print("-" * 70)
134
135 # Create a simple associative memory
136 # Bind objects to their properties
137 hot = model.random(seed=10)
138 cold = model.random(seed=11)
139
140 fire_is_hot = model.bind(model.random(seed=20), hot) # fire → hot
141 ice_is_cold = model.bind(model.random(seed=21), cold) # ice → cold
142
143 # Bundle related facts into knowledge base
144 knowledge = model.bundle([fire_is_hot, ice_is_cold])
145
146 print("Created knowledge base:")
147 print(f" 'fire is hot' + 'ice is cold' → single hypervector")
148 print()
149
150 # Query: What is fire?
151 fire = model.random(seed=20)
152 fire_property = model.unbind(knowledge, fire)
153
154 sim_hot = model.similarity(fire_property, hot)
155 sim_cold = model.similarity(fire_property, cold)
156
157 print("Query: What property does fire have?")
158 print(f" Similarity to 'hot': {sim_hot:.3f} ← Answer!")
159 print(f" Similarity to 'cold': {sim_cold:.3f}")
160 print()
161
162 # ============================================================================
163 # Summary
164 # ============================================================================
165 print("=" * 70)
166 print("Summary: What You've Learned")
167 print("=" * 70)
168 print()
169 print("✓ Created a 10,000-dimensional VSA model")
170 print("✓ Encoded symbolic data as hypervectors")
171 print("✓ Composed structured representations with binding")
172 print("✓ Retrieved information through unbinding")
173 print("✓ Encoded continuous values with similarity preservation")
174 print("✓ Built a simple associative memory")
175 print()
176 print("Next steps:")
177 print(" → 01_basic_operations.py - Deep dive into VSA operations")
178 print(" → 02_models_comparison.py - Compare different VSA models")
179 print(" → 10_encoders_scalar.py - Master continuous value encoding")
180 print()
181 print("Full documentation: https://docs.holovecai.com")
182 print("=" * 70)