Skip to content

Instantly share code, notes, and snippets.

@willzeng
Created November 5, 2019 17:41
Show Gist options
  • Select an option

  • Save willzeng/f55dc6d412fd1d94005ab1c60a6fc685 to your computer and use it in GitHub Desktop.

Select an option

Save willzeng/f55dc6d412fd1d94005ab1c60a6fc685 to your computer and use it in GitHub Desktop.
Function for calculating the cross-entropy benchmarking fidelity
def fidelity_xeb(n_qubits: int, trials: int, n_samples: int, sampler: Callable[[np.ndarray, int], float]) -> float:
dim = 2**n_qubits
# keep track of the ideal simulated probabilities
ideal_probs = []
# loop over the random programs
for _ in range(trials):
unitary = random_unitary(dim)
sample_probs = [sampler(unitary, bb) for bb in range(dim)]
samples = np.random.choice(dim, size=n_samples, p=sample_probs)
for sample in samples:
ideal_prob = simulate_probability(unitary, sample)
ideal_probs.append(ideal_prob)
return dim*np.mean(ideal_probs) - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment