quantastica / quantum-circuit

Quantum Circuit Simulator implemented in JavaScript
MIT License
243 stars 47 forks source link

feature "export to Cirq" #7

Closed snuffkin closed 5 years ago

snuffkin commented 5 years ago

I implemented the feature of "export to Cirq". However, the manner of implementation may be different that this project intends. Therefore, there is no problem even if this pull request is not accepted.

Run various_gates.js to check this feature.

import cirq
import numpy as np

def u2(p_phi, p_lambda):
    return cirq.SingleQubitMatrixGate(np.array([[1/np.sqrt(2), -np.exp(1j*p_lambda)/np.sqrt(2)], [np.exp(1j*p_phi)/np.sqrt(2), np.exp(1j*p_lambda+1j*p_phi)/np.sqrt(2)]]))

def u3(p_theta, p_phi, p_lambda):
    return cirq.SingleQubitMatrixGate(np.array([[np.cos(p_theta/2), -np.exp(1j*p_lambda)*np.sin(p_theta/2)], [np.exp(1j*p_phi)*np.sin(p_theta/2), np.exp(1j*p_lambda+1j*p_phi)*np.cos(p_theta/2)]]))

q = [cirq.NamedQubit('q' + str(i)) for i in range(2)]

circuit = cirq.Circuit.from_ops(
    cirq.Rz(0)(q[0]),
    cirq.X(q[0]),
    cirq.Y(q[0]),
    cirq.Z(q[0]),
    cirq.H(q[0]),
    (cirq.X**(1/2))(q[0]),
    cirq.Rz(np.pi / 2)(q[0]),
    cirq.Rz(np.pi / 4)(q[0]),
    cirq.Rz(np.pi / 8)(q[0]),
    cirq.Rx(np.pi / 2)(q[0]),
    cirq.Ry(np.pi / 2)(q[0]),
    cirq.Rz(np.pi / 2)(q[0]),
    cirq.Rz(np.pi / 2)(q[0]),
    u2(np.pi / 2, np.pi / 4)(q[0]),
    u3(np.pi / 2, np.pi / 4, np.pi / 8)(q[0]),
    cirq.S(q[0]),
    cirq.T(q[0]),
    cirq.Rz(-np.pi)(q[0]),
    cirq.Rz(-np.pi / 2)(q[0]),
    cirq.SWAP(q[0], q[1]),
    (cirq.SWAP**(1/2))(q[0], q[1]),
    cirq.CNOT(q[0], q[1]),
    cirq.measure(q[0], key='c0'),
    cirq.measure(q[0], key='c1'),
)

simulator = cirq.google.XmonSimulator()
result = simulator.run(circuit)
print(result)
perak commented 5 years ago

@snuffkin WOW! This is perfect!!! <3

I will merge later when I reach my computer (4-5 hours after now).

Also, I will update script that generates README.md and extend qps-client to allow running circuits on cirq simulator from UI.

Thank you!!!