MCPcopy Create free account
hub / github.com/davisking/dlib / apply_gate_to

Method apply_gate_to

dlib/quantum_computing/quantum_computing.h:277–327  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

275 }
276
277 void apply_gate_to (quantum_register& reg) const
278 {
279 // make sure requires clause is not broken
280 DLIB_CASSERT(reg.num_bits() == num_bits,
281 "\tvoid gate_exp::apply_gate_to()"
282 << "\n\tinvalid arguments to this function"
283 << "\n\treg.num_bits(): " << reg.num_bits()
284 << "\n\tnum_bits: " << num_bits
285 << "\n\tthis: " << this
286 );
287
288
289 quantum_register temp(reg);
290
291
292 // check if any of the elements of the register are 1 and if so then
293 // we don't have to do the full matrix multiply. Or check if only a small number are non-zero.
294 long non_zero_elements = 0;
295 for (long r = 0; r < dims; ++r)
296 {
297 if (reg.state_vector()(r) != qc_scalar_type(0))
298 ++non_zero_elements;
299
300 reg.state_vector()(r) = 0;
301 }
302
303
304 if (non_zero_elements > 3)
305 {
306 // do a full matrix multiply to compute the output state
307 for (long r = 0; r < dims; ++r)
308 {
309 reg.state_vector()(r) = compute_state_element(temp.state_vector(),r);
310 }
311 }
312 else
313 {
314 // do a matrix multiply but only use the columns in the gate matrix
315 // that correspond to the non-zero register elements
316 for (long r = 0; r < dims; ++r)
317 {
318 if (temp.state_vector()(r) != qc_scalar_type(0))
319 {
320 for (long i = 0; i < dims; ++i)
321 {
322 reg.state_vector()(i) += temp.state_vector()(r)*exp(i,r);
323 }
324 }
325 }
326 }
327 }
328
329 template <typename exp>
330 qc_scalar_type compute_state_element (

Callers 3

shor_encodeFunction · 0.80
shor_decodeFunction · 0.80
mainFunction · 0.80

Calls 2

expFunction · 0.85
num_bitsMethod · 0.80

Tested by

no test coverage detected