The Classical Shadow of Unknown Quantum States¶

Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.

Overview¶

For the quantum state ρρ in an unknown quantum system, obtaining the information it contains is a fundamental and essential problem. This tutorial will discuss how to use the classical shadow to describe an unknown quantum state with classical data so that many quantum state properties can be efficiently estimated. In the era of NISQ (noisy intermediate-scale quantum), the classical shadow helps us trade quantum for classical resources. After describing the information of the quantum state with classical data, some quantum problems can be solved by methods such as classical machine learning. And using this method, the cost of the quantum circuit running times in some existing variational quantum algorithms like variational quantum eigensolver (VQE) may be reduced so that an acceleration can be achieved.

The Classical Shadow¶

The intuition of the classical shadow comes from shadows in real life. If we use a beam of light to illuminate a polyhedron vertically, we get a shadow of it. Rotating this polyhedron, we can see its different shadows. After many rotations, this series of shadows naturally reflect some information about this polyhedron. Similarly, in the quantum world, if we perform a unitary transformation on a quantum state and then perform a measurement, can we also get a "shadow" of the quantum state? The construction of the classical shadow is analogous to this example. Its construction process is as follows:

First, we apply a unitary transformation to an unknown quantum state ρρ in an nn qubits system: ρ↦UρU†ρ↦UρU†, and then measure each qubit in the computational basis. For the measurement result, we use |^b⟩|b^⟩ as an example. We perform the inverse transformation of the previous unitary on |^b⟩|b^⟩ to get U†|^b⟩⟨^b|UU†|b^⟩⟨b^|U. We know that the expectation of the measured quantum state |^b⟩⟨^b||b^⟩⟨b^| is:

E(|^b⟩⟨^b|)=∑b∈{0,1}nPr(|^b⟩⟨^b|=|b⟩⟨b|)⋅|b⟩⟨b|=∑b∈{0,1}n⟨b|UρU†|b⟩|b⟩⟨b|.(1)(1)E(|b^⟩⟨b^|)=∑b∈{0,1}nPr⁡(|b^⟩⟨b^|=|b⟩⟨b|)⋅|b⟩⟨b|=∑b∈{0,1}n⟨b|UρU†|b⟩|b⟩⟨b|.

Then after the reverse operation, the expectation value of U†|^b⟩⟨^b|UU†|b^⟩⟨b^|U is ∑b∈{0,1}n⟨b|UρU†|b⟩U†|b⟩⟨b|U∑b∈{0,1}n⟨b|UρU†|b⟩U†|b⟩⟨b|U. In this process, the unitary transformation UU is randomly selected from a fixed set. When we repeat this process and average UU, we can get:

EU∼U(n)(E(U†|^b⟩⟨^b|U))=∑b∈{0,1}nEU∼U(n)(⟨b|UρU†|b⟩U†|b⟩⟨b|U)(2)(2)EU∼U(n)(E(U†|b^⟩⟨b^|U))=∑b∈{0,1}nEU∼U(n)(⟨b|UρU†|b⟩U†|b⟩⟨b|U)

where U(n)U(n) is a given unitary transformation set on nn qubits.

If this expectation value is recorded as M(ρ)M(ρ), then MM will be a map from ρρ to M(ρ)M(ρ). When MM is linear and reversible [1], the initial quantum state ρρ can be expressed as

ρ=M−1(EU∼U(n)(E(U†|^b⟩⟨^b|U)))=EU∼U(n)(E(M−1(U†|^b⟩⟨^b|U))).(3)(3)ρ=M−1(EU∼U(n)(E(U†|b^⟩⟨b^|U)))=EU∼U(n)(E(M−1(U†|b^⟩⟨b^|U))).

With M−1M−1, every time UU is sampled, we compute ^ρ=M−1(U†|^b⟩⟨^b|U)ρ^=M−1(U†|b^⟩⟨b^|U) and name it a snapshot. After repeating this process NN times, we get NN collections of snapshots about ρρ:

S(ρ;N)={^ρ1=M−1(U†1|^b1⟩⟨^b1|U1),…,^ρN=M−1(U†N|^bN⟩⟨^bN|UN)}.(4)(4)S(ρ;N)={ρ^1=M−1(U1†|b^1⟩⟨b^1|U1),…,ρ^N=M−1(UN†|b^N⟩⟨b^N|UN)}.

We call S(ρ;N)S(ρ;N) the classical shadow of ρρ. It is worth mentioning that what exactly MM is depends on the sampling set UU we select. For example, when we select the Clifford group as the sampling set UU, we have:

M(ρ)=EU∼Cl(n)(E(U†|^b⟩⟨^b|U))=12n+1ρ+12n+1I.(5)(5)M(ρ)=EU∼Cl⁡(n)(E(U†|b^⟩⟨b^|U))=12n+1ρ+12n+1I.

Readers may refer to [1] for details about why MM is as (5). It follows that

M−1(12n+1ρ+12n+1I)=ρ⇒M−1(ρ)=(2n+1)ρ−I.(6)(6)M−1(12n+1ρ+12n+1I)=ρ⇒M−1(ρ)=(2n+1)ρ−I.

After constructing the classical shadow, how does it help us to effectively estimate the properties of the quantum state? Some linear properties of quantum state ρρ are very suitable to be estimated by the classical shadow, for example, the expectation value of ρρ of an observable OO: o=tr(Oρ)o=tr⁡(Oρ) [1]. Let ^o=tr(O^ρ)o^=tr⁡(Oρ^), then according to (3), there is E[^o]=tr(Oρ)E[o^]=tr⁡(Oρ). We provide detailed applications and implementations in our following tutorial: Estimation of Quantum State Properties Based on the Classical Shadow.

Next, we will show the process of constructing the classical shadow for a randomly selected quantum state ρρ in Paddle Quantum to help readers understand how the classical shadow works. And we will use the Clifford group as the sampling set of the unitary transformation (specifically about the properties of the Clifford group and how to sample uniformly distributed Clifford operators from it randomly, readers can refer to [1], [3], and the Clifford class in Paddle Quantum).

Paddle Quantum Implementation¶

First, we need to import all the dependencies:

In [3]:
import numpy as np
import paddle
import matplotlib.pyplot as plt
import paddle_quantum as pq
from paddle_quantum.qinfo import trace_distance
from paddle_quantum.state import to_state

Next, we randomly generate a quantum state ρρ.

In [4]:
# Number of qubits
n_qubit = 2
pq.set_backend('density_matrix')

# Randomly generate a state 
rho_random = pq.state.random_state(n_qubit, rank=1) 

# Define |0> and |1>
ket_0 = np.array([[1,0]]).T
ket_1 = np.array([[0,1]]).T

# Define I and coefficient
I = np.eye(2**n_qubit)
coefficient = float(2**n_qubit + 1)

In the next step, we define the circuit required for the measurement and prepare us to record the results.

In [5]:
def measure_by_clifford(phi, num_qubit):
    """
    After applying Clifford operation on rho, obtain measurement in the computational basis
    """
    # Use the Clifford class of Paddle Quantum to randomly select a Clifford operator and generate its circuit
    clif = pq.gate.clifford.Clifford(num_qubit)
    # Run the circuit
    cir = clif.circuit()
    # Get the unitary implemented by the circuit
    cl = cir.unitary_matrix().numpy()
    # Single measurement
    result = cir(phi).measure(shots=1)
    bitstring = [k for k, v in result.items() if v == 1.0]

    # Use this to record results of measurement
    bhat = [[1.]]
    for i in bitstring[0]:
        if i == '0':
            bhat = np.kron(bhat, ket_0)
        elif i == '1':
            bhat = np.kron(bhat, ket_1)
    return bhat, cl

Sampling unitary and building the classical shadow¶

In [17]:
# Select the number of samples
S = 800

estimator_rho = []
tracedistance = []

for sample in range(S):
    
    bhat, cl = measure_by_clifford(rho_random, n_qubit)

    # Get the shadow according to the deduced M inverse
    hat_rho = coefficient * cl.conj().T @ bhat @ bhat.T @ cl - I
    estimator_rho.append(hat_rho)
    
    # Compute the average of the shadows 
    # Since in actual operation, we cannot achieve the expectation value in (3), we can only approximate rho by averaging the classical shadow obtained.
    ave_estimate = sum(estimator_rho) / len(estimator_rho)    
    # Calculate trace distance
    tracedistance.append(trace_distance(rho_random, to_state(paddle.to_tensor(ave_estimate))).numpy())

Finally, we output the matrix representation of an approximation of ρρ by the classical shadow and the real ρρ. Also, we output their trace distance. The closer the trace distance is to 0, the closer the approximation is to the real quantum state.

In [18]:
print('Approximation: ', np.around(ave_estimate, decimals=3))
print('-' * 50)
print('Real state: ', np.around(rho_random.numpy(), decimals=3))
print('Trace distance between approximation and real state: ', np.around(tracedistance[-1][0], decimals=3))
Approximation:  [[0.434+0.j    0.319-0.084j 0.392+0.045j 0.161-0.008j]
 [0.319+0.084j 0.178+0.j    0.286+0.033j 0.211+0.048j]
 [0.392-0.045j 0.286-0.033j 0.356+0.j    0.25 +0.009j]
 [0.161+0.008j 0.211-0.048j 0.25 -0.009j 0.031+0.j   ]]
--------------------------------------------------
Real state:  [[0.419+0.j    0.27 -0.036j 0.38 +0.057j 0.142+0.036j]
 [0.27 +0.036j 0.178+0.j    0.24 +0.07j  0.088+0.035j]
 [0.38 -0.057j 0.24 -0.07j  0.353+0.j    0.133+0.013j]
 [0.142-0.036j 0.088-0.035j 0.133-0.013j 0.051+0.j   ]]
Trace distance between approximation and real state:  [0.225]

As you can see, our approximation of ρρ is very close to the real quantum state ρρ in the matrix representation. With about 800 samplings, the trace distance between the approximation and the real state is already around 0.2. The relation between their trace distance and the number of samplings is shown below.

In [19]:
# Print out the result
fig,ax = plt.subplots(figsize=(10, 10))
 
plt.xlabel('number of samples', fontsize=18)
plt.ylabel('trace distance', fontsize=18)
j = range(len(tracedistance)) 
plt.plot(j, tracedistance, 'r', label="trace_distance")
"""open the grid"""
plt.grid(True)
plt.legend(fontsize=14)
plt.show()
No description has been provided for this image

Conclusion¶

This tutorial introduces some theoretical knowledge of the classical shadow. In this example, we constructed the classical shadow of a random 2-qubit quantum state in Paddle Quantum. We can intuitively understand that the classical shadow can make a good approximation to an unknown quantum state. In fact, [2] pointed out demanding full classical descriptions of quantum systems may be excessive for many concrete tasks. Instead, it is often sufficient to accurately predict certain properties of the quantum system. This is where the fundamental importance of the classical shadow lies. Another tutorial (Estimation of Quantum State Properties Based on the Classical Shadow) will continue to introduce the applications of the classical shadow and how to use the shadow function in Paddle Quantum.

References¶

[1] Huang, Hsin-Yuan, Richard Kueng, and John Preskill. "Predicting many properties of a quantum system from very few measurements." Nature Physics 16.10 (2020): 1050-1057.

[2] Aaronson, Scott. "Shadow tomography of quantum states." SIAM Journal on Computing 49.5 (2019): STOC18-368.

[3] Bravyi, Sergey, and Dmitri Maslov. "Hadamard-free circuits expose the structure of the Clifford group." IEEE Transactions on Information Theory 67.7 (2021): 4546-4563.