Skip to content

Quantum Machine Learning Constructors

Besides the arbitrary Hamiltonian constructors, Qadence also provides a complete set of program constructors useful for digital-analog quantum machine learning programs.

Feature Maps

A few feature maps are directly available for feature loading,

from qadence import feature_map

n_qubits = 3

fm = feature_map(n_qubits, fm_type="fourier")

fm = feature_map(n_qubits, fm_type="chebyshev")

fm = feature_map(n_qubits, fm_type="tower")
Fourier = KronBlock(0,1,2) [tag: FM]
├── RX(0) [params: ['phi']]
├── RX(1) [params: ['phi']]
└── RX(2) [params: ['phi']]
Chebyshev KronBlock(0,1,2) [tag: FM]
├── RX(0) [params: ['2*acos(phi)']]
├── RX(1) [params: ['2*acos(phi)']]
└── RX(2) [params: ['2*acos(phi)']]
Tower KronBlock(0,1,2) [tag: FM]
├── RX(0) [params: ['2*acos(phi)']]
├── RX(1) [params: ['4*acos(phi)']]
└── RX(2) [params: ['6*acos(phi)']]

Hardware-Efficient Ansatz

Ansatze blocks for quantum machine-learning are typically built following the Hardware-Efficient Ansatz formalism (HEA). Both fully digital and digital-analog HEAs can easily be built with the hea function. By default, the digital version is returned:

from qadence import hea
from qadence.draw import display

n_qubits = 3
depth = 2

ansatz = hea(n_qubits, depth)
%3 11f0663d19b6471c9fb77d5d84fb4471 0 98441540942d4319a93462736c537233 RX(theta₀) 11f0663d19b6471c9fb77d5d84fb4471--98441540942d4319a93462736c537233 3c183c56aeb34c98b8217475cb7a73ff 1 f59aec44209b44018d2c92eb031e63c6 RY(theta₃) 98441540942d4319a93462736c537233--f59aec44209b44018d2c92eb031e63c6 6abcb1c2d9844dfca62dfba4de1809e4 RX(theta₆) f59aec44209b44018d2c92eb031e63c6--6abcb1c2d9844dfca62dfba4de1809e4 3dc65f416c144f328fb9c0a5d8b4d698 6abcb1c2d9844dfca62dfba4de1809e4--3dc65f416c144f328fb9c0a5d8b4d698 3e6f507c28444b1ca4f5202c4a30d607 3dc65f416c144f328fb9c0a5d8b4d698--3e6f507c28444b1ca4f5202c4a30d607 5328dc434bb4469091871e4009bed4d3 RX(theta₉) 3e6f507c28444b1ca4f5202c4a30d607--5328dc434bb4469091871e4009bed4d3 32f87769899842bc893be1b2d7848acb RY(theta₁₂) 5328dc434bb4469091871e4009bed4d3--32f87769899842bc893be1b2d7848acb f417fde3c30841a7842519e2379ac207 RX(theta₁₅) 32f87769899842bc893be1b2d7848acb--f417fde3c30841a7842519e2379ac207 a0aa51e274b848719cb8174f22f059bc f417fde3c30841a7842519e2379ac207--a0aa51e274b848719cb8174f22f059bc 9e5ba65ea935431c88e8746c10c72890 a0aa51e274b848719cb8174f22f059bc--9e5ba65ea935431c88e8746c10c72890 5dfa9202258f4a839a37ff4004f5a5d8 9e5ba65ea935431c88e8746c10c72890--5dfa9202258f4a839a37ff4004f5a5d8 06fbd84c85554665a62c73a4072f2e1e 50d7b183143b4d8c9a7f8a8e3bc8baea RX(theta₁) 3c183c56aeb34c98b8217475cb7a73ff--50d7b183143b4d8c9a7f8a8e3bc8baea 8e45e357116b4fbdb0cc3295c37c429f 2 25b340d31dc64ff09f70fd47675890e5 RY(theta₄) 50d7b183143b4d8c9a7f8a8e3bc8baea--25b340d31dc64ff09f70fd47675890e5 3f3438b4e5c446bdb08810820e035c68 RX(theta₇) 25b340d31dc64ff09f70fd47675890e5--3f3438b4e5c446bdb08810820e035c68 95e0aaadb01943e5b5aa842fdfe7d3ae X 3f3438b4e5c446bdb08810820e035c68--95e0aaadb01943e5b5aa842fdfe7d3ae 95e0aaadb01943e5b5aa842fdfe7d3ae--3dc65f416c144f328fb9c0a5d8b4d698 71ecfc1355294367bfe2e5f7ad04d154 95e0aaadb01943e5b5aa842fdfe7d3ae--71ecfc1355294367bfe2e5f7ad04d154 159bfbe5d56346f886b27d3f660d4199 RX(theta₁₀) 71ecfc1355294367bfe2e5f7ad04d154--159bfbe5d56346f886b27d3f660d4199 4124e4f59c3f4f42a1443a9d959db814 RY(theta₁₃) 159bfbe5d56346f886b27d3f660d4199--4124e4f59c3f4f42a1443a9d959db814 2decf678f3bf47a8a7f76823c78ce9e4 RX(theta₁₆) 4124e4f59c3f4f42a1443a9d959db814--2decf678f3bf47a8a7f76823c78ce9e4 6922d5d00f3f4955952c76d4329532f5 X 2decf678f3bf47a8a7f76823c78ce9e4--6922d5d00f3f4955952c76d4329532f5 6922d5d00f3f4955952c76d4329532f5--a0aa51e274b848719cb8174f22f059bc 167e5af1572b44e2ac3cd1d8b2b720d8 6922d5d00f3f4955952c76d4329532f5--167e5af1572b44e2ac3cd1d8b2b720d8 167e5af1572b44e2ac3cd1d8b2b720d8--06fbd84c85554665a62c73a4072f2e1e ae918bfb099346e5a13a42871536efc1 707410e69fb446389679c0d677bbba70 RX(theta₂) 8e45e357116b4fbdb0cc3295c37c429f--707410e69fb446389679c0d677bbba70 ce5ba182da584ec3be3bf5e60df05b1e RY(theta₅) 707410e69fb446389679c0d677bbba70--ce5ba182da584ec3be3bf5e60df05b1e 34293be03d424401918cd71681e4ce5c RX(theta₈) ce5ba182da584ec3be3bf5e60df05b1e--34293be03d424401918cd71681e4ce5c 4e898d7b7fee428b8a10c605988c3e15 34293be03d424401918cd71681e4ce5c--4e898d7b7fee428b8a10c605988c3e15 d66200b665af4c84820aca4950633688 X 4e898d7b7fee428b8a10c605988c3e15--d66200b665af4c84820aca4950633688 d66200b665af4c84820aca4950633688--71ecfc1355294367bfe2e5f7ad04d154 acbbe78f6f034d9e91514f67bf323caf RX(theta₁₁) d66200b665af4c84820aca4950633688--acbbe78f6f034d9e91514f67bf323caf bc64c4c79e3245ebaebd1be34909dd87 RY(theta₁₄) acbbe78f6f034d9e91514f67bf323caf--bc64c4c79e3245ebaebd1be34909dd87 09f6cce9bcea43dfb191e17f7a7d8067 RX(theta₁₇) bc64c4c79e3245ebaebd1be34909dd87--09f6cce9bcea43dfb191e17f7a7d8067 0464fb6b3d5849f394d34bb3fcf9c733 09f6cce9bcea43dfb191e17f7a7d8067--0464fb6b3d5849f394d34bb3fcf9c733 12447a5eadc64736a718545612568fb0 X 0464fb6b3d5849f394d34bb3fcf9c733--12447a5eadc64736a718545612568fb0 12447a5eadc64736a718545612568fb0--167e5af1572b44e2ac3cd1d8b2b720d8 12447a5eadc64736a718545612568fb0--ae918bfb099346e5a13a42871536efc1

As seen above, the rotation layers are automatically parameterized, and the prefix "theta" can be changed with the param_prefix argument.

Furthermore, both the single-qubit rotations and the two-qubit entangler can be customized with the operations and entangler argument. The operations can be passed as a list of single-qubit rotations, while the entangler should be either CNOT, CZ, CRX, CRY, CRZ or CPHASE.

from qadence import RX, RY, CPHASE

ansatz = hea(
    n_qubits=n_qubits,
    depth=depth,
    param_prefix="phi",
    operations=[RX, RY, RX],
    entangler=CPHASE
)
%3 85162bbc45584a56bbe50378b758dfc9 0 d2f5129c2d1e4d2a83f1d166cc94c122 RX(phi₀) 85162bbc45584a56bbe50378b758dfc9--d2f5129c2d1e4d2a83f1d166cc94c122 8e3724f8e9d144b8b1759c5585fa2320 1 94d51bbff1694241ad5ba1f6d056a22c RY(phi₃) d2f5129c2d1e4d2a83f1d166cc94c122--94d51bbff1694241ad5ba1f6d056a22c 452bcf99eb984065b6f05490d09c3f63 RX(phi₆) 94d51bbff1694241ad5ba1f6d056a22c--452bcf99eb984065b6f05490d09c3f63 a659be2eb0fa42c7a5e30403cced4283 452bcf99eb984065b6f05490d09c3f63--a659be2eb0fa42c7a5e30403cced4283 189a88f7cb94468b900ed67dd58ca5f0 a659be2eb0fa42c7a5e30403cced4283--189a88f7cb94468b900ed67dd58ca5f0 0d29ca6da1d54baab5faa949eff11d7e RX(phi₉) 189a88f7cb94468b900ed67dd58ca5f0--0d29ca6da1d54baab5faa949eff11d7e 867fa1fe36bf4e8ba89d1ae888810ad6 RY(phi₁₂) 0d29ca6da1d54baab5faa949eff11d7e--867fa1fe36bf4e8ba89d1ae888810ad6 c92f292ca3f64a6a859d48856c84f73b RX(phi₁₅) 867fa1fe36bf4e8ba89d1ae888810ad6--c92f292ca3f64a6a859d48856c84f73b e2b735a48c3f43c9aaeebf168ac4c9c4 c92f292ca3f64a6a859d48856c84f73b--e2b735a48c3f43c9aaeebf168ac4c9c4 0ae0df5af4b9453fbd0333e5829f9f55 e2b735a48c3f43c9aaeebf168ac4c9c4--0ae0df5af4b9453fbd0333e5829f9f55 78238345188f4097b73aa90e5da480a3 0ae0df5af4b9453fbd0333e5829f9f55--78238345188f4097b73aa90e5da480a3 f81d45e45da742e98abc21c2d6b3daa1 3197580b870d44cb846e8ff73d6ad73f RX(phi₁) 8e3724f8e9d144b8b1759c5585fa2320--3197580b870d44cb846e8ff73d6ad73f 6f460e832e5c4d85a52b5ff04c9c368a 2 bd72b29a49de4683bcd45eb8b4cb192d RY(phi₄) 3197580b870d44cb846e8ff73d6ad73f--bd72b29a49de4683bcd45eb8b4cb192d 694402cc6cd94ee08f75674d5af0c6fd RX(phi₇) bd72b29a49de4683bcd45eb8b4cb192d--694402cc6cd94ee08f75674d5af0c6fd 3fb3e72c8f614d2ca7461661eb8be639 PHASE(phi_ent₀) 694402cc6cd94ee08f75674d5af0c6fd--3fb3e72c8f614d2ca7461661eb8be639 3fb3e72c8f614d2ca7461661eb8be639--a659be2eb0fa42c7a5e30403cced4283 cef166efe55347ed9b578537161deb3b 3fb3e72c8f614d2ca7461661eb8be639--cef166efe55347ed9b578537161deb3b 12da0cd420a54f4e8ade93ef9f3db98a RX(phi₁₀) cef166efe55347ed9b578537161deb3b--12da0cd420a54f4e8ade93ef9f3db98a 186de9dcb7984aeea518bcc6817e9f2f RY(phi₁₃) 12da0cd420a54f4e8ade93ef9f3db98a--186de9dcb7984aeea518bcc6817e9f2f 2b406562873b434ca050d3a98fb039e1 RX(phi₁₆) 186de9dcb7984aeea518bcc6817e9f2f--2b406562873b434ca050d3a98fb039e1 2f3890894de84a54aabc236166749469 PHASE(phi_ent₂) 2b406562873b434ca050d3a98fb039e1--2f3890894de84a54aabc236166749469 2f3890894de84a54aabc236166749469--e2b735a48c3f43c9aaeebf168ac4c9c4 7fc64e27a2044e909819f6e77255ccfd 2f3890894de84a54aabc236166749469--7fc64e27a2044e909819f6e77255ccfd 7fc64e27a2044e909819f6e77255ccfd--f81d45e45da742e98abc21c2d6b3daa1 6c85f86fddb6453c88a450d42d817cb9 03685e933968479fb1d07461e09a14f4 RX(phi₂) 6f460e832e5c4d85a52b5ff04c9c368a--03685e933968479fb1d07461e09a14f4 40dd0569982446948ab741d8aa47c058 RY(phi₅) 03685e933968479fb1d07461e09a14f4--40dd0569982446948ab741d8aa47c058 755fda1715154917ab68f72af96c1fad RX(phi₈) 40dd0569982446948ab741d8aa47c058--755fda1715154917ab68f72af96c1fad b8a39805d2fe4b2ca9a3a209298a3081 755fda1715154917ab68f72af96c1fad--b8a39805d2fe4b2ca9a3a209298a3081 c738260b190840d0a2f11fc11779c055 PHASE(phi_ent₁) b8a39805d2fe4b2ca9a3a209298a3081--c738260b190840d0a2f11fc11779c055 c738260b190840d0a2f11fc11779c055--cef166efe55347ed9b578537161deb3b 0cf3ff3b7dc04486803172f1ff5a8155 RX(phi₁₁) c738260b190840d0a2f11fc11779c055--0cf3ff3b7dc04486803172f1ff5a8155 df4fe6aead0c40e89446f2b044cc7e88 RY(phi₁₄) 0cf3ff3b7dc04486803172f1ff5a8155--df4fe6aead0c40e89446f2b044cc7e88 a65a97869bb642ed91955561c19d9323 RX(phi₁₇) df4fe6aead0c40e89446f2b044cc7e88--a65a97869bb642ed91955561c19d9323 c82b2115f78042088b92169b1f3258dc a65a97869bb642ed91955561c19d9323--c82b2115f78042088b92169b1f3258dc ce6b70ced9c34774b2f7d0868a93bb6a PHASE(phi_ent₃) c82b2115f78042088b92169b1f3258dc--ce6b70ced9c34774b2f7d0868a93bb6a ce6b70ced9c34774b2f7d0868a93bb6a--7fc64e27a2044e909819f6e77255ccfd ce6b70ced9c34774b2f7d0868a93bb6a--6c85f86fddb6453c88a450d42d817cb9

Having a truly hardware-efficient ansatz means that the entangling operation can be chosen according to each device's native interactions. Besides digital operations, in Qadence it is also possible to build digital-analog HEAs with the entanglement produced by the natural evolution of a set of interacting qubits, as is natural in neutral atom devices. As with other digital-analog functions, this can be controlled with the strategy argument which can be chosen from the Strategy enum type. Currently, only Strategy.DIGITAL and Strategy.SDAQC are available. By default, calling strategy = Strategy.SDAQC will use a global entangling Hamiltonian with Ising-like NN interactions and constant interaction strength inside a HamEvo operation,

from qadence import Strategy

ansatz = hea(
    n_qubits,
    depth=depth,
    strategy=Strategy.SDAQC
)
%3 cluster_19edd8da2df0494d951b1198b26070fa cluster_2808bcf21f9f45848bb53bb09c1050fd 8d2dbed2d4c44f56888ab631d87392de 0 bd65751c8ca44b55b74522eee8b65409 RX(theta₀) 8d2dbed2d4c44f56888ab631d87392de--bd65751c8ca44b55b74522eee8b65409 4bcae4867caf4af2b14f24d59d552935 1 79fbfc96e0e042cf8479562f5a0df741 RY(theta₃) bd65751c8ca44b55b74522eee8b65409--79fbfc96e0e042cf8479562f5a0df741 9f03819d1c7f493eab58a88c919ba8bb RX(theta₆) 79fbfc96e0e042cf8479562f5a0df741--9f03819d1c7f493eab58a88c919ba8bb 8f1ae458556641e5b7d1dfe8ad5d5967 HamEvo 9f03819d1c7f493eab58a88c919ba8bb--8f1ae458556641e5b7d1dfe8ad5d5967 07997c067ffb44f194c6b4f2b0ba0714 RX(theta₉) 8f1ae458556641e5b7d1dfe8ad5d5967--07997c067ffb44f194c6b4f2b0ba0714 f347f7739a8b4590a88fa9272da9a84a RY(theta₁₂) 07997c067ffb44f194c6b4f2b0ba0714--f347f7739a8b4590a88fa9272da9a84a bc955ff1cb5241499c666865e29d6d11 RX(theta₁₅) f347f7739a8b4590a88fa9272da9a84a--bc955ff1cb5241499c666865e29d6d11 831e72e443a34bfaa8d8f9a9715a676b HamEvo bc955ff1cb5241499c666865e29d6d11--831e72e443a34bfaa8d8f9a9715a676b badc14d969f04829846332816e761b67 831e72e443a34bfaa8d8f9a9715a676b--badc14d969f04829846332816e761b67 9bf5a4df9a8c4840bcb04b2940d15320 e246ff143fd64146839bc35698f00d93 RX(theta₁) 4bcae4867caf4af2b14f24d59d552935--e246ff143fd64146839bc35698f00d93 cc91a1167d1e49658be970508b13e7eb 2 e94350db19c742ed89fc0f76ecb7bee0 RY(theta₄) e246ff143fd64146839bc35698f00d93--e94350db19c742ed89fc0f76ecb7bee0 534c4d4e3fe141f99ca226bb17bdbfdd RX(theta₇) e94350db19c742ed89fc0f76ecb7bee0--534c4d4e3fe141f99ca226bb17bdbfdd 48cfc8abde5c4b8ca9af1044aa9a99b6 t = theta_t₀ 534c4d4e3fe141f99ca226bb17bdbfdd--48cfc8abde5c4b8ca9af1044aa9a99b6 22b85c90ace7483691c6af7e24db5925 RX(theta₁₀) 48cfc8abde5c4b8ca9af1044aa9a99b6--22b85c90ace7483691c6af7e24db5925 8a4989c5a053474a8b4a017d1c398644 RY(theta₁₃) 22b85c90ace7483691c6af7e24db5925--8a4989c5a053474a8b4a017d1c398644 027aa78166b849c897625fa95965533a RX(theta₁₆) 8a4989c5a053474a8b4a017d1c398644--027aa78166b849c897625fa95965533a 877c63505d74465a911e0f05239c80cc t = theta_t₁ 027aa78166b849c897625fa95965533a--877c63505d74465a911e0f05239c80cc 877c63505d74465a911e0f05239c80cc--9bf5a4df9a8c4840bcb04b2940d15320 ee7ed2807d6348e385ad88b603fa8dce f04c6da8ab0a4bf6bd299794626e4082 RX(theta₂) cc91a1167d1e49658be970508b13e7eb--f04c6da8ab0a4bf6bd299794626e4082 9b17b258029a44e492c279aef3577b6f RY(theta₅) f04c6da8ab0a4bf6bd299794626e4082--9b17b258029a44e492c279aef3577b6f 311d6031fbad465e9026424d23fb8cbe RX(theta₈) 9b17b258029a44e492c279aef3577b6f--311d6031fbad465e9026424d23fb8cbe 12c761b2e5f94a2bb97faa1583de2e05 311d6031fbad465e9026424d23fb8cbe--12c761b2e5f94a2bb97faa1583de2e05 c9ac03cb9339422ca7f3147a2585b591 RX(theta₁₁) 12c761b2e5f94a2bb97faa1583de2e05--c9ac03cb9339422ca7f3147a2585b591 1d66b33453874873b5349cba24087569 RY(theta₁₄) c9ac03cb9339422ca7f3147a2585b591--1d66b33453874873b5349cba24087569 2179f44618884138bafddffbe505a234 RX(theta₁₇) 1d66b33453874873b5349cba24087569--2179f44618884138bafddffbe505a234 d1fa1f7184404a9397cea25128e4f0ef 2179f44618884138bafddffbe505a234--d1fa1f7184404a9397cea25128e4f0ef d1fa1f7184404a9397cea25128e4f0ef--ee7ed2807d6348e385ad88b603fa8dce

Note that, by default, only the time-parameter is automatically parameterized when building a digital-analog HEA. However, as described in the Hamiltonians tutorial, arbitrary interaction Hamiltonians can be easily built with the hamiltonian_factory function, with both customized or fully parameterized interactions, and these can be directly passed as the entangler for a customizable digital-analog HEA.

from qadence import hamiltonian_factory, Interaction, N, Register, hea

# Build a parameterized neutral-atom Hamiltonian following a honeycomb_lattice:
register = Register.honeycomb_lattice(1, 1)

entangler = hamiltonian_factory(
    register,
    interaction=Interaction.NN,
    detuning=N,
    interaction_strength="e",
    detuning_strength="n"
)

# Build a fully parameterized Digital-Analog HEA:
n_qubits = register.n_qubits
depth = 2

ansatz = hea(
    n_qubits=register.n_qubits,
    depth=depth,
    operations=[RX, RY, RX],
    entangler=entangler,
    strategy=Strategy.SDAQC
)
%3 cluster_61425953959b447cb7c3145974dd2fe4 cluster_ad806daa582347719ba44cf1228bd289 d4b973bb6a404d10b01d213037a1af36 0 fb194e6d108a4ad4a90d7ff42ced7cf2 RX(theta₀) d4b973bb6a404d10b01d213037a1af36--fb194e6d108a4ad4a90d7ff42ced7cf2 b28a21886c3c4dcdbbe3e0ad169d38db 1 844b75817c664809b470ad1c5218949d RY(theta₆) fb194e6d108a4ad4a90d7ff42ced7cf2--844b75817c664809b470ad1c5218949d b2127ce4ce5045098ef0d9b7ba83cf53 RX(theta₁₂) 844b75817c664809b470ad1c5218949d--b2127ce4ce5045098ef0d9b7ba83cf53 2d58dbfc67264a6e86729c0f92d651ba b2127ce4ce5045098ef0d9b7ba83cf53--2d58dbfc67264a6e86729c0f92d651ba 25e0c0bade54442e9316fac9d97db709 RX(theta₁₈) 2d58dbfc67264a6e86729c0f92d651ba--25e0c0bade54442e9316fac9d97db709 1f72b72853154a579d6bf8a5b50d140a RY(theta₂₄) 25e0c0bade54442e9316fac9d97db709--1f72b72853154a579d6bf8a5b50d140a 822cc376f9164244838fc82e0866db34 RX(theta₃₀) 1f72b72853154a579d6bf8a5b50d140a--822cc376f9164244838fc82e0866db34 8b85e7aaa4404beaba162742e5821871 822cc376f9164244838fc82e0866db34--8b85e7aaa4404beaba162742e5821871 cc90a34317bb412ba2c9162978a296c1 8b85e7aaa4404beaba162742e5821871--cc90a34317bb412ba2c9162978a296c1 271b5517893d447cba34ff4e5e08f60a 420f1cf5a2ce41d089b708fece50eeed RX(theta₁) b28a21886c3c4dcdbbe3e0ad169d38db--420f1cf5a2ce41d089b708fece50eeed 992bf3b0cbee4e868904475eb92f567c 2 37f3b135fd0c4f13a3de3ed8bf7de78a RY(theta₇) 420f1cf5a2ce41d089b708fece50eeed--37f3b135fd0c4f13a3de3ed8bf7de78a d46a60c2e2814335a02c6ecedaf1ad6e RX(theta₁₃) 37f3b135fd0c4f13a3de3ed8bf7de78a--d46a60c2e2814335a02c6ecedaf1ad6e 0a695a09dd2146319912d11872ebad9d d46a60c2e2814335a02c6ecedaf1ad6e--0a695a09dd2146319912d11872ebad9d 9354ad22f0c64e369d6d571610e571e1 RX(theta₁₉) 0a695a09dd2146319912d11872ebad9d--9354ad22f0c64e369d6d571610e571e1 318a1b6fb0ab46fea7c25b00c7c37146 RY(theta₂₅) 9354ad22f0c64e369d6d571610e571e1--318a1b6fb0ab46fea7c25b00c7c37146 0043538e9c9c485391f80b7e04fd457e RX(theta₃₁) 318a1b6fb0ab46fea7c25b00c7c37146--0043538e9c9c485391f80b7e04fd457e 48e4ded5652746bbb2f55df70c29e875 0043538e9c9c485391f80b7e04fd457e--48e4ded5652746bbb2f55df70c29e875 48e4ded5652746bbb2f55df70c29e875--271b5517893d447cba34ff4e5e08f60a 5c0fe756360e41189d010a5496379f3b 5c2a6f6df89744c682b29bb663fcf17b RX(theta₂) 992bf3b0cbee4e868904475eb92f567c--5c2a6f6df89744c682b29bb663fcf17b e1006349c1b441de9c5ee02716e07568 3 d22460c236e9456ca05eb37da0d6f81e RY(theta₈) 5c2a6f6df89744c682b29bb663fcf17b--d22460c236e9456ca05eb37da0d6f81e b219d33d438a4ff88da52b6471613414 RX(theta₁₄) d22460c236e9456ca05eb37da0d6f81e--b219d33d438a4ff88da52b6471613414 9023343a34564235b30044775ad1e03f HamEvo b219d33d438a4ff88da52b6471613414--9023343a34564235b30044775ad1e03f 979af5292c1646bc98a23cd05194305b RX(theta₂₀) 9023343a34564235b30044775ad1e03f--979af5292c1646bc98a23cd05194305b 111441a9356443f6a1f1803cf61395da RY(theta₂₆) 979af5292c1646bc98a23cd05194305b--111441a9356443f6a1f1803cf61395da 0c75b6a7b5704a879a801ebb81e1b09c RX(theta₃₂) 111441a9356443f6a1f1803cf61395da--0c75b6a7b5704a879a801ebb81e1b09c b33d8cb360aa4093b92acd73b9629493 HamEvo 0c75b6a7b5704a879a801ebb81e1b09c--b33d8cb360aa4093b92acd73b9629493 b33d8cb360aa4093b92acd73b9629493--5c0fe756360e41189d010a5496379f3b 585fbfbacaf14cbbb0a49e560a7b0383 e4b3737b00f64fafa34354285717f4bd RX(theta₃) e1006349c1b441de9c5ee02716e07568--e4b3737b00f64fafa34354285717f4bd 9becc7533f02436fb406f5b8aa8e6a7a 4 57761921522f4be19cd5569687b22311 RY(theta₉) e4b3737b00f64fafa34354285717f4bd--57761921522f4be19cd5569687b22311 d09480580a484e53b0009da608f6df4c RX(theta₁₅) 57761921522f4be19cd5569687b22311--d09480580a484e53b0009da608f6df4c 9c4f6dcb0c064b99b74e524769c0e183 t = theta_t₀ d09480580a484e53b0009da608f6df4c--9c4f6dcb0c064b99b74e524769c0e183 251485eb4f33421d960c2da10ad1fcd3 RX(theta₂₁) 9c4f6dcb0c064b99b74e524769c0e183--251485eb4f33421d960c2da10ad1fcd3 9c015523296f4214b00bb156d85f5a47 RY(theta₂₇) 251485eb4f33421d960c2da10ad1fcd3--9c015523296f4214b00bb156d85f5a47 dddde1a5671549ce9487dbfcf7ccbb7e RX(theta₃₃) 9c015523296f4214b00bb156d85f5a47--dddde1a5671549ce9487dbfcf7ccbb7e 25c8cae953494f228849fd36e6e4c62d t = theta_t₁ dddde1a5671549ce9487dbfcf7ccbb7e--25c8cae953494f228849fd36e6e4c62d 25c8cae953494f228849fd36e6e4c62d--585fbfbacaf14cbbb0a49e560a7b0383 a151ac2342614addaafc726dbb7024f6 61620cced5b34c2185e13b0e9656c4e9 RX(theta₄) 9becc7533f02436fb406f5b8aa8e6a7a--61620cced5b34c2185e13b0e9656c4e9 be825d22604d4ccfb9e2bcc0e1923f6e 5 93a05487ab424e52a08cc58275a35881 RY(theta₁₀) 61620cced5b34c2185e13b0e9656c4e9--93a05487ab424e52a08cc58275a35881 fda6dcac659046e3922887485bf9a3e3 RX(theta₁₆) 93a05487ab424e52a08cc58275a35881--fda6dcac659046e3922887485bf9a3e3 b17de94f215e480d849c4ec356b3b3d8 fda6dcac659046e3922887485bf9a3e3--b17de94f215e480d849c4ec356b3b3d8 cc7e9f3ebcab4395bc79fc26d83bcbe8 RX(theta₂₂) b17de94f215e480d849c4ec356b3b3d8--cc7e9f3ebcab4395bc79fc26d83bcbe8 9f1307ea8b43481fad67b83cdb48e90b RY(theta₂₈) cc7e9f3ebcab4395bc79fc26d83bcbe8--9f1307ea8b43481fad67b83cdb48e90b 2582cf18b06f4ad58b15262f6f103d53 RX(theta₃₄) 9f1307ea8b43481fad67b83cdb48e90b--2582cf18b06f4ad58b15262f6f103d53 a96877724c9749a294f96345b986b53c 2582cf18b06f4ad58b15262f6f103d53--a96877724c9749a294f96345b986b53c a96877724c9749a294f96345b986b53c--a151ac2342614addaafc726dbb7024f6 0538a5ac3e1c4a6c866691d88b2d312e 1bd0939339a44516936d361680469544 RX(theta₅) be825d22604d4ccfb9e2bcc0e1923f6e--1bd0939339a44516936d361680469544 3121bfd5e2f74d45a8326e8d698664a3 RY(theta₁₁) 1bd0939339a44516936d361680469544--3121bfd5e2f74d45a8326e8d698664a3 8690b391f56e4838a65ec8a9d8ecfc51 RX(theta₁₇) 3121bfd5e2f74d45a8326e8d698664a3--8690b391f56e4838a65ec8a9d8ecfc51 6b8d45b637cb4a0e842561a8b847635e 8690b391f56e4838a65ec8a9d8ecfc51--6b8d45b637cb4a0e842561a8b847635e 218632e25e7f4dfc9f6eae37614d23c0 RX(theta₂₃) 6b8d45b637cb4a0e842561a8b847635e--218632e25e7f4dfc9f6eae37614d23c0 fc6bae226607473b9c7b7d0f5eb24ed9 RY(theta₂₉) 218632e25e7f4dfc9f6eae37614d23c0--fc6bae226607473b9c7b7d0f5eb24ed9 687d5353b0324cd5b260acf7c6ef1d0c RX(theta₃₅) fc6bae226607473b9c7b7d0f5eb24ed9--687d5353b0324cd5b260acf7c6ef1d0c 30db378315724cfbaf6ed35d7e5e023e 687d5353b0324cd5b260acf7c6ef1d0c--30db378315724cfbaf6ed35d7e5e023e 30db378315724cfbaf6ed35d7e5e023e--0538a5ac3e1c4a6c866691d88b2d312e
Qadence also offers a out-of-the-box training routine called train_with_grad for optimizing fully-differentiable models like QNNs and QuantumModels containing either trainable and/or non-trainable parameters (i.e., inputs). Feel free to refresh your memory about different parameter types.

Machine Learning Tools

train_with_grad performs training, logging/printing loss metrics and storing intermediate checkpoints of models.

As every other training routine commonly used in Machine Learning, it requires model, data and an optimizer as input arguments. However, in addition, it requires a loss_fn and a TrainConfig. A loss_fn is required to be a function which expects both a model and data and returns a tuple of (loss, metrics: <dict>), where metrics is a dict of scalars which can be customized too.

import torch
from itertools import count
cnt = count()
criterion = torch.nn.MSELoss()

def loss_fn(model: torch.nn.Module, data: torch.Tensor) -> tuple[torch.Tensor, dict]:
    next(cnt)
    x, y = data[0], data[1]
    out = model(x)
    loss = criterion(out, y)
    return loss, {}

The TrainConfig [qadence.ml_tools.config] tells train_with_grad what batch_size should be used, how many epochs to train, in which intervals to print/log metrics and how often to store intermediate checkpoints.

from qadence.ml_tools import TrainConfig

batch_size = 5
n_epochs = 100

config = TrainConfig(
    folder="some_path/",
    max_iter=n_epochs,
    checkpoint_every=100,
    write_every=100,
    batch_size=batch_size,
)

Fitting a funtion with a QNN using ml_tools

Let's look at a complete example of how to use train_with_grad now.

from pathlib import Path
import torch
from itertools import count
from qadence.constructors import hamiltonian_factory, hea, feature_map
from qadence import chain, Parameter, QuantumCircuit, Z
from qadence.models import QNN
from qadence.ml_tools import train_with_grad, TrainConfig
import matplotlib.pyplot as plt

n_qubits = 2
fm = feature_map(n_qubits)
ansatz = hea(n_qubits=n_qubits, depth=3)
observable = hamiltonian_factory(n_qubits, detuning=Z)
circuit = QuantumCircuit(n_qubits, fm, ansatz)

model = QNN(circuit, observable, backend="pyqtorch", diff_mode="ad")
batch_size = 1
input_values = {"phi": torch.rand(batch_size, requires_grad=True)}
pred = model(input_values)

cnt = count()
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)

def loss_fn(model: torch.nn.Module, data: torch.Tensor) -> tuple[torch.Tensor, dict]:
    next(cnt)
    x, y = data[0], data[1]
    out = model(x)
    loss = criterion(out, y)
    return loss, {}

tmp_path = Path("/tmp")

n_epochs = 5

config = TrainConfig(
    folder=tmp_path,
    max_iter=n_epochs,
    checkpoint_every=100,
    write_every=100,
    batch_size=batch_size,
)

batch_size = 25

x = torch.linspace(0, 1, batch_size).reshape(-1, 1)
y = torch.sin(x)

train_with_grad(model, (x, y), optimizer, config, loss_fn=loss_fn)

plt.plot(y.numpy())
plt.plot(model(input_values).detach().numpy())

For users who want to use the low-level API of qadence, here is the example from above written without train_with_grad.

Fitting a function - Low-level API

from pathlib import Path
import torch
from itertools import count
from qadence.constructors import hamiltonian_factory, hea, feature_map
from qadence import chain, Parameter, QuantumCircuit, Z
from qadence.models import QNN
from qadence.ml_tools import train_with_grad, TrainConfig

n_qubits = 2
fm = feature_map(n_qubits)
ansatz = hea(n_qubits=n_qubits, depth=3)
observable = hamiltonian_factory(n_qubits, detuning=Z)
circuit = QuantumCircuit(n_qubits, fm, ansatz)

model = QNN(circuit, observable, backend="pyqtorch", diff_mode="ad")
batch_size = 1
input_values = {"phi": torch.rand(batch_size, requires_grad=True)}
pred = model(input_values)

criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)
n_epochs=50
cnt = count()

tmp_path = Path("/tmp")

config = TrainConfig(
    folder=tmp_path,
    max_iter=n_epochs,
    checkpoint_every=100,
    write_every=100,
    batch_size=batch_size,
)

x = torch.linspace(0, 1, batch_size).reshape(-1, 1)
y = torch.sin(x)

for i in range(n_epochs):
    out = model(x)
    loss = criterion(out, y)
    loss.backward()
    optimizer.step()