My Quantum Journey #01: Qubit Probability Amplitude Calculator in R

July 07, 2026 • 0 comments

Original image, made in GIMP, of ket notation psi, as a depiction of calculating the qubit probability amplitude in quantum computing, made possible by KrisComputes!

First blog post talking all math and computation, and I can already hear them—"Kris, good sir. Why on earth would you use R for quantum computation?" I'm glad you (never) asked because the reason is simple: I'm finding R to be a wonderful programming language. It's clean and straight-forward; that's why I decided to jump on in and write with it—of all languages. For that, let me show you what I came up with.


"Equationism"

We know what a probability amplitude is, and it's written in this formula:

$$\ket{\psi} = \begin{bmatrix} \alpha \\ \beta \end{bmatrix} = \alpha\ket{0} + \beta\ket{1}$$

(Reminder: this isn't a tutorial.)

For \(\alpha\) and \(\beta\), they are the absolute value of complex numbers—squared:

$$|\alpha|^2 + |\beta|^2$$

All good in the neighborhood. Granted, I personally do not own any quantum measuring device(s)—would like to own one someday, depending on price—of any kind, so I'm going off on examples common in calculating these. Having known this, I thought, "let's write a program for it." Since these things are easily done on a calculator, I wanted to craft a program doing so.

For that, I wrote this:

                                      

    M <- matrixInput()

    sqMatrix <- M^2
    cat("Squared the entered elements gives you:\n")
    print(sqMatrix)

    elemSummed <- sum(sqMatrix)
    cat("Adding them together sums to:\n")
    print(elemSummed)

    finalized <- elemSummed

    if (abs(finalized - 1) < 1e-9) {
        print("System is normalized.")
    } else {
        print("System is not normalized.")
    }
                                      
                                    

Originally, my "first draft" code looked like this:

                                      
    squared_matrix <- M^2

    cat("When elements are squared, we get:\n")
    print(squared_matrix)

    addedElem <- sum(squared_matrix)
    cat("When added together after squaring, we get:\n")
    print(addedElem)

    finalized <- addedElem

    if (finalized == 1) {
        print("System is normalized.")
    } else {
        print("System is not normalized.")
    }
                                      
                                    

By the way, I defined variable M as:

                                      
    M <- matrix(0, nrow = 2, ncol = 1)
                                      
                                    

Basically, I wrote the program asking for two elements before calculation. As I was writing, I was thinking, "well, it's easy to craft a pre-made calculator that prints out answers upon run time. How about making it interactive?" Behold:

                                      
matrixInput <- function() {
    M <- matrix(0, nrow = 2, ncol = 1)

    cat("Calculating the probability of a state being either |0> or |1>:\n\n")

    repeat {
        cat("Enter first complex value--floating number:\n")
        value1 <- readLines("stdin", n=1)
        number1 <- suppressWarnings(as.numeric(value1))
        if(!is.na(number1)) {
            M[1, 1] <- number1
            cat("First element: ", value1, "\n\n")
            break
        } else {
            cat("Invalid number, Enter a numeric value.\n")
        }
    }

    repeat {
        cat("Now, enter the second complex value--floating number:\n")
        value2 <- readLines("stdin", n=1)
        number2 <- suppressWarnings(as.numeric(value2))
        if(!is.na(number2)) {
            M[2, 1] <- number2
            cat("Second element: ", value2, "\n\n")
            break
        } else {
            cat("Invalid number, Enter a numeric value.\n")
        }
    }
    return(M)

                                      
                                    

However, since we are dealing with complex numbers, it made sense to change as.numeric to as.complex, and so:

                                      

matrixInput <- function() {
    M <- matrix(0, nrow = 2, ncol = 1)

    cat("Calculating the probability of a state being either |0> or |1>:\n\n")

    repeat {
        cat("Enter first complex value--floating number:\n")
        value1 <- readLines("stdin", n=1)
        number1 <- suppressWarnings(as.complex(value1))
        if(!is.na(number1)) {
            M[1, 1] <- number1
            cat("First element: ", value1, "\n\n")
            break
        } else {
            cat("Invalid number, Enter a numeric value.\n")
        }
    }

    repeat {
        cat("Now, enter the second complex value--floating number:\n")
        value2 <- readLines("stdin", n=1)
        number2 <- suppressWarnings(as.complex(value2))
        if(!is.na(number2)) {
            M[2, 1] <- number2
            cat("Second element: ", value2, "\n\n")
            break
        } else {
            cat("Invalid number, Enter a numeric value.\n")
        }
    }
    return(M)
}

                                      
                                    

That's right: I crafted the program to ask the user, themselves, to input the values to be calculated. I did this since not every probability amplitude is \((\frac{\sqrt{3}}{2})^2\) nor \((\frac{1}{2})^2\). Speaking of which, as shown previously, I had M^2 to implement the square of the amplitudes. Again, with respect to the "complexities" of complex numbers, I used the modulo function:

                                      

    Mod(M)^2

                                      
                                    

Take user input for both numeric and complex values for \(\alpha\) and \(\beta\), do the calculation, and print out the answer—plus a message letting the user know if the system is normalized or not. After, the program then asks the user if they want to calculate more or not; if "y," then the program repeats, and if "n," the program closes. That's very much it!

You're wondering, "but Kris, are those loops necessary?" I treated this like an actual calculator: a program with "replay" value, if you will. Now, yes, I can write a program declaring variables with pre-assigned values, then run it to see the answer. That's fine too, but for me, I like that replay value. That way, you can always use it if more calculations are needed. Syntactically speaking, R has one of the most reader-friendly code I've ever seen (I say the same for MATLAB). As plenty have said, being able to read (your) code is important; and that's what going on here, influencing my choice. I only speak of my experience and knowledge so far, but what do you think?

"R" Like a Pirate!

It's exhausting enough with those wanting to defend their beloved programming language(s), starting unnecessary arguments with others. Use whatever programming language you want; but arguments by malice against those who love and enjoy what they like is not worth having high blood pressure over—think about that. Writing with R makes my health very stable; the code is neat and pleasing to the eyes; I had no problem writing with it and enjoyed the process. There are plenty of math-related libraries R has, as I haven't used them yet. In terms of quantum computing, it may not be the choice to consider, but something as simple as this calculation, which can be done on any programming language, I did so in R to solidify my learning of the language. I haven't planned a project with R yet, but I'll definitely consider this language to be one of my main choices going forward. There'll be plenty more where this came from! I'm thankful for R, their communities, and all developers; this includes the designers themselves Ross Ihaka and Robert Gentleman!


Origins

Being this is the very first blog post, per Math & Computation section, I want to take this time introducing my origins behind me looking to work and contribute in the world of everything quantum (mechanics, computing, etc). I've been self-taught in video production since I was 14; producing my own projects for 20+ years. In contrast, I didn't discover computer programming—coding—until 2009, when I was 22-23 years old. I discovered it as a means to create a more "dynamic" website for my video portfolio, until I veered off seeing the fun, joy, and problem-solving aspect of it. It was challenging, but I loved it. Staunched in my disdain for mathematics, programming remains my key reason I circled back and came to love math. Fifteen years later, after losing my job to AI in 2024, I went back to school. I earned my BA in mathematics in 2025 with a GPA of 3.6: the highest grade point average in all my entire academic career. (I thrive better when I'm self-taught; so much so that I self-published a narrative essay about it.) Even as I write this, I see my stacks of math books on both my left and right sides of me; I can access, read, re-learn, and practice any math concepts as I wish, from calculus to differential equations, from mathematical logic to topology (long live physical media!). I love math, and I can say it—likely—saved my career—perhaps my life.

The quantum aspects of my learning actually started when I was 15 years old. After finding out that one eighties song I recalled when I was about 3 years old, hearing it on the radio during a family roadtrip through Mojave Desert, I found that that artist was Tears For Fears. After high school, I spent a lot of my time exploring, and even memorizing, Tears For Fears' entire album library of songs; to this day, I'm still a huge fan. What does an eighties, British artist, Tears For Fears, have anything to do with quantum mechanics/computing? Believe it or not, they have a song titled Schrödinger's Cat (you can listen to it here on YouTube). In my life so far, I've heard this song played on the radio once and never heard it again. I find Tears For Fears to be those rare artists with an acquired taste: despite having big hit songs, like Everybody Wants To Rule The World, Head Over Heels, and Shout, they're a duo where listening to their music/writing style with an open mind is required—especially for their later albums. Yes, I can succinctly, albeit proudly, say that a song inspired me to explore, and immediately love, quantum mechanics. Thank you, Tears For Fears!

During the lockdowns of 2020, I made my time productive by signing up with edX.org around October 2020. There, I earned a certificate in quantum computing basics with DelftX in February 2021. It was my first exposure to advanced mathematical concepts, such as tensor algebra, but the challenge and learning was all worth it. After going back to school in 2024, I looked no further: gathering all pieces, tying up loose ends, this is the pathway I long to be in. If it contains math, and can write programs for it, I'm interested; in ties with that amazing song from Tears For Fears, anything quantum, data extraction, and calculation are my current focus; thanks to my strong, eternal love for math.

Thank you all for choosing to stick around with me in this journey!

I'm Kris Caballero, and this is what I computed today.


Comments

Add Comment

* Required information
1000
Drag & drop images (max 3)
Enter the word shark backwards.
Captcha Image

Comments

No comments yet. Be the first!