Youtube - Lightning Talk Quantum Interpretations of the C++ Object Model - David Stone - CppCon 2021

Lightning Talk: Quantum Interpretations of the C++ Object Model - David Stone - CppCon 2021

https://www.youtube.com/watch?v=hWPohuUR0e4

Transcrição

[00:10]

00:10 quantum interpretations of the c plus object model there's a handheld mic somewhere oh okay much better

[00:20]

00:20 so prior to c plus plus 20 all c plus plus programs were undefined since c plus plus 20 they're probably still undefined but at least not for lifetime reasons

[00:31]

00:31 so first the basics uh i expect you all pretty much know this so you know tree bqp uh is a subset of the third level of the polynomial hierarchy ph uh you

[00:42]

00:42 know cetera et cetera reza's technique the permanent the determinant of a matrix you know all this stuff whatever i know what you're thinking obviously um now for those of you who don't

[00:51]

00:51 understand quantum mechanics that's fine we're going to talk about c plus from here on out uh so construction versus assignment in c plus plus there's a very simple rule

[01:00]

01:00 you can follow okay the assignment operator never constructs objects okay well not not never so you can also use an assignment via direct union access but that's simple

[01:11]

01:11 right uh well okay so it doesn't always you know sometimes there's some restrictions you know what let's let's just move on okay whatever who cares let's see some

[01:20]

01:20 code okay so your code was busted prior to c plus 20. this code here undefined behavior and the reason it was undefined behavior was because malloc created uh allocated memory but there weren't

[01:31]

01:31 actually any objects there and you're just assigning an int to some memory okay so this was bad this was undefined behavior everybody did this because you could have done this malloc

[01:41]

01:41 has no way of knowing that you're trying to do an int you could have said size of float you could have said four how does malloc know that there should be ins there it doesn't

[01:48]

01:48 uh essentially like you know you look at this code this code is the code that we don't want to have defined behavior because you're saying oh i allocated

[01:56]

01:56 memory it doesn't hit there oh actually no it has a float there that doesn't work that violates the strict aliasing rule of c plus which is actually useful for optimizations and program

[02:03]

02:03 understanding uh it's essentially the same as this code here the previous slide constructed in it you can't just cast it to a float that's a reinterpret cast

[02:12]

02:12 and in fact all of your code was busted the same was true if you went through the standard allocator interface everyone's code was busted this was undefined behavior

[02:21]

02:21 because this was undefined behavior because even though you could reinterpret cast an inch to an unsigned char there wasn't actually an array of unsigned char objects there so when you

[02:30]

02:30 increment the pointer twice you've incremented past the one past the end of the one object you actually were able to reinterpret cast to this was bad

[02:39]

02:39 because it meant that this was undefined behavior it was impossible to implement vector in c plus prior to c plus plus 20. so how did c plus plus 20 fix this

[02:50]

02:50 it added implicit lifetime types implicit lifetime types are types that have a trivial constructor and a trivial destructor and it means that lifetime creation or

[03:02]

03:02 lifetime of these implicit lifetime types begins when you create an array of bytes but which lifetime begins when you create an array of bytes that's the

[03:11]

03:11 question so to understand this this is where quantum mechanics comes in first the copenhagen interpretation uh so named because uh biannual strip was

[03:21]

03:21 born in denmark three hours outside of copenhagen uh but the ahasa interpretation didn't seem like it would roll off the tongue quite as well so they called it the copenhagen

[03:28]

03:28 interpretation uh basically it says that variables can exist at a superposition of multiple possible type states it's focused on what type is this variable uh you can't

[03:39]

03:39 actually say you have to observe it and when you observe the type this collapses the aliasing function and that forces the type to be the type you observed it as

[03:49]

03:49 that's one interpretation or there's the many programs interpretation this is the one i prefer it basically says that creation of an array of bytes causes decoherence

[03:59]

03:59 and then this creates an infinite number of program states across an infinite number of abstract machines one for each possible set of types

[04:09]

04:09 and each one exists in its own abstract machine the important thing here is that undefined behavior destroys the abstract machine or the universe

[04:18]

04:18 this is a form of quantum immortality basically saying that programs that contain defined behavior survive and those that do not are destroyed this is the origin of the term program execution

[04:30]

04:30 because most programs are executed [Music] if no abstract machines remain the behavior of your program is undefined now

[04:39]

04:39 i'd like to show you schrodinger's cat uh named for the unix utility cat that outputs a value uh and you don't know what value it's going to output because that depends on the type so here we

[04:51]

04:51 create an array of bytes and then get a random number that cannot be predicted true hardware randomness and then we either cast that to a pointer to int and assign to it or we cast it to a pointer

[05:02]

05:02 to float and assign through it now both interpretations say that this is well-defined code under the copenhagen interpretation uh the array of bytes uh you know it has some sort of unknown

[05:14]

05:14 state and then uh you know we do this but now you can expand it a little bit you can have the double slit experiment just by changing the types a little bit not too exciting

[05:22]

05:22 uh so what can start a lifetime of an object we can uh mem copy functions uh our mem copy mem move these maintain the existing type or we can have type changing copying functions like bitcast

[05:31]

05:31 these copy the bits maintaining the representation but they change the type okay so there's mem copy in the no cloning theorem basically in this example here we know that the type of a

[05:42]

05:42 is int and this means that the type of b isn't because the two states are entangled okay so what objects exist [Music]

[05:50]

05:50 in this example we say into x equals five okay my time is up here uh so i guess that's all i have thank you all [Applause] you