API Reference#

diffqc Package#

diffqc: Differentiable Quantum Circuit Simulator#

See also

diffqc.dense

Densely Represented Operations

diffqc.sparse

Sparsely Represented Operations

diffqc.lib

Builtin Algorithms

diffqc.nn

Builtin Neural Network Modules

diffqc.util

Utility Functions

diffqc.pennylane

PennyLane Plugin

Examples

>>> import jax.numpy as jnp
>>> from diffqc import dense as op
>>> q0 = op.zeros(2, jnp.complex64) # |00>
>>> op.expectZ(q0, (0,))
1
>>> q1 = op.PauliX(q0, (0,))        # |10>
>>> op.expectZ(q1, (0,))
-1
>>> qh = op.Hadamard(q0, (0,))      # (|00> + |10>)/sqrt(2)
>>> qhcnot = op.CNOT(qh, (0, 1))    # (|00> + |11>)/sqrt(2)
>>> op.expectZ(qhcnot, (1,))
0

Functions#

expval(probs, wire)

Expectation of |1>

marginal_prob(probs, integrate_wires)

Calculate marginal probabilities

prob(state[, dtype])

Calculate probabilities for n-qubit state

sample(key, probs, shape)

Sample proportional to probabilities

diffqc.dense Module#

Densely Represented Operations (diffqc.dense)#

Core operations with internal dense representation.

See also

diffqc.sparse

Sparsely Represented Operations

Notes

Internally probability amplitudes of all possible 2**(qubits) quantum states are recorded as an array with (2, 2, ..., 2) shape.

Functions#

zeros(nqubits, dtype)

Create |00...0> state

to_state(x)

Convert qubits internal representation to state vector

expectX(c, wires)

Expectation of X measurement

expectY(c, wires)

Expectation of Y measurement

expectZ(c, wires)

Expectation of Z measurement

expectUnitary(c, wires, U)

Expectation of Unitary measurement

Hadamard(c, wires)

Apply Hadamard Gate

PauliX(c, wires)

Apply Pauli X Gate

PauliY(c, wires)

Apply Pauli Y Gate

PauliZ(c, wires)

Apply Pauli Z Gate

S(c, wires)

Apply S Gate (Single qubite Phase Gate)

T(c, wires)

Apply T Gate

SX(c, wires)

Apply Square Root X Gate

CNOT(c, wires)

Apply controlled-NOT Gate

CZ(c, wires)

Apply controlled-Z Gate

CY(c, wires)

Apply controlled-Y Gate

SWAP(c, wires)

Apply SWAP Gate

ISWAP(c, wires)

Apply i-SWAP Gate

ECR(c, wires)

Apply Echoed RZX(pi/2) Gate

SISWAP(c, wires)

Apply Square Root i-SWAP Gate

SQISWAP(c, wires)

Apply Square Root i-SWAP Gate

CSWAP(c, wires)

Apply controlled SWAP Gate

Toffoli(c, wires)

Apply Toffoli Gate (controlled controlled X Gate)

Rot(c, wires, phi, theta, omega)

Apply Rotation Gate

RX(c, wires, phi)

Apply Rotation X Gate

RY(c, wires, phi)

Apply Rotation Y Gate

RZ(c, wires, phi)

Apply Rotation Z Gate

PhaseShift(c, wires, phi)

Apply Local Phase Shift Gate

ControlledPhaseShift(c, wires, phi)

Apply controlled Phase Shift Gate

CPhase(c, wires, phi)

Apply controlled Phase Shift Gate

CPhaseShift00(c, wires, phi)

Apply Phase Shift Gate for |00>

CPhaseShift01(c, wires, phi)

Apply Phase Shift Gate for |01>

CPhaseShift10(c, wires, phi)

Apply Phase Shift Gate for |10>

CRX(c, wires, phi)

Apply controlled RX Gate

CRY(c, wires, phi)

Apply controlled RY Gate

CRZ(c, wires, phi)

Apply controlled RZ Gate

CRot(c, wires, phi, theta, omega)

Apply controlled Rotation Gate

U1(c, wires, phi)

Apply Local Phase Shift Gate

U2(c, wires, phi, delta)

Apply controlled U2 Gate

U3(c, wires, theta, phi, delta)

Apply U3 Gate

PSWAP(c, wires, phi)

Apply Phase SWAP Gate

RXX(c, wires, theta)

Rotate XX (exp(-iXX * theta))

RYY(c, wires, theta)

Rotate YY (exp(-iYY * theta))

RZZ(c, wires, theta)

Rotate ZZ (exp(-iZZ * theta))

QubitUnitary(c, wires, U)

Unitary Gate

ControlledQubitUnitary(c, wires, U)

Controlled Unitary Gate

diffqc.sparse Module#

Sparsely Represented Operations (diffqc.sparse)#

Core operations with internal sparse representation.

See also

diffqc.dense

Densely Represented Operations

Notes

Internally probability ampulitudes of possible quantum states are recorded as an array with (superpositions, qubits, 2) shape.

Warning

Since JAX requires static shape, we cannot remove zero-amplitude quantum states from superposition. Moreover, same quantum states might be traced separately.

Functions#

zeros(nqubits, dtype)

Create |00...0> state

to_state(x)

Convert qubits internal representation to state vector

expectX(c, wires)

Expectation of X measurement

expectY(c, wires)

Expectation of Y measurement

expectZ(c, wires)

Expectation of Z measurement

expectUnitary(c, wires, U)

Expectation of Unitary measurement

Hadamard(c, wires)

Apply Hadamard Gate

PauliX(c, wires)

Apply Pauli X Gate

PauliY(c, wires)

Apply Pauli Y Gate

PauliZ(c, wires)

Apply Pauli Z Gate

S(c, wires)

Apply S Gate (Single qubite Phase Gate)

T(c, wires)

Apply T Gate

SX(c, wires)

Apply Square Root X Gate

CNOT(c, wires)

Apply controlled-NOT Gate

CZ(c, wires)

Apply controlled-Z Gate

CY(c, wires)

Apply controlled-Y Gate

SWAP(c, wires)

Apply SWAP Gate

ISWAP(c, wires)

Apply i-SWAP Gate

ECR(c, wires)

Apply Echoed RZX(pi/2) Gate

SISWAP(c, wires)

Apply Square Root i-SWAP Gate

SQISWAP(c, wires)

Apply Square Root i-SWAP Gate

CSWAP(c, wires)

Apply controlled SWAP Gate

Toffoli(c, wires)

Apply Toffoli Gate (controlled controlled X Gate)

Rot(c, wires, phi, theta, omega)

Apply Rotation Gate

RX(c, wires, phi)

Apply Rotation X Gate

RY(c, wires, phi)

Apply Rotation Y Gate

RZ(c, wires, phi)

Apply Rotation Z Gate

PhaseShift(c, wires, phi)

Apply Local Phase Shift Gate

ControlledPhaseShift(c, wires, phi)

Apply controlled Phase Shift Gate

CPhase(c, wires, phi)

Apply controlled Phase Shift Gate

CPhaseShift00(c, wires, phi)

Apply Phase Shift Gate for |00>

CPhaseShift01(c, wires, phi)

Apply Phase Shift Gate for |01>

CPhaseShift10(c, wires, phi)

Apply Phase Shift Gate for |10>

CRX(c, wires, phi)

Apply controlled RX Gate

CRY(c, wires, phi)

Apply controlled RY Gate

CRZ(c, wires, phi)

Apply controlled RZ Gate

CRot(c, wires, phi, theta, omega)

Apply controlled Rotation Gate

U1(c, wires, phi)

Apply Local Phase Shift Gate

U2(c, wires, phi, delta)

Apply controlled U2 Gate

U3(c, wires, theta, phi, delta)

Apply U3 Gate

PSWAP(c, wires, phi)

Apply Phase SWAP Gate

RXX(c, wires, theta)

Rotate XX (exp(-iXX * thta))

RYY(c, wires, theta)

Rotate YY (exp(-iYY * thta))

RZZ(c, wires, theta)

Rotate ZZ (exp(-iZZ * thta))

QubitUnitary(c, wires, U)

Unitary Gate

ControlledQubitUnitary(c, wires, U)

Controlled Unitary Gate

diffqc.lib Module#

Builtin Algorithms (diffqc.lib)#

Notes

To support multiple internal representations, operation module (aka. diffqc.dense or diffqc.sparse) is passed.

Functions#

GHZ(op, c, wires)

Create Greenberger-Horne-Zeilinger state from |00...0> state

HHL(op, c, wires, U, aux, anc)

Solving Linear Equation with Harrow-Hassidim-Lloyd Algorithm

QFT(op, c, wires)

Quantum Fourier Transform

QPE(op, c, wires, U, aux)

Quantum Phase Estimation

diffqc.nn Module#

Builtin Neural Network Modules (diffqc.nn)#

Notes

To support multiple internal representations, operation module (aka. diffqc.dense or diffqc.sparse) is passed.

Functions#

CircuitCentricBlock(op, c, wires, weights)

Apply Circuit Centric Block as Parameterized Quantum Circuit (PQC)

Convolution(op, kernel_func, kernel_shape, ...)

Create Convolution Function

JosephsonSampler(op, c, wires, weights)

Apply Josephson Sampler as Parameterized Quantum Circuit (PQC)

MaxPooling(x, shape)

Max Pooling for 2D

diffqc.util Module#

Utility Functions (diffqc.util)#

Notes

To support multiple internal representations, operation module (aka. diffqc.dense or diffqc.sparse) is passed.

Functions#

CreateMatrix(op, nqubits, dtype, f)

Create Transition Matrix from function

CreatePossibleState(op, nqubits, dtype)

Create all possible state

diffqc.pennylane Module#

PennyLane Plugin (diffqc.pennylane)#

PennyLane [1] plugin providing an device with diffqc.

Warning

This module is still under development, and missing some features.

References

1

https://pennylane.ai/

Examples

>>> import pennylane as qml
>>> dev = qml.device("diffqc.qubit", wires=2, mode="dense")

Classes#

diffqcQubitDevice(wires, ...)

Class Inheritance Diagram#

Inheritance diagram of diffqc.pennylane.diffqcQubitDevice