A study of the CP properties of the interaction between the Higgs boson and top quarks is presented. Higgs bosons are identified via the diphoton decay channel () and their production in association with a top quark pair () or single top quark () is studied. The analysis uses 139 fb of protonproton collision data recorded at a center-of-mass energy of = 13 TeV with the ATLAS detector at the Large Hadron Collider. It is performed using a fit to the diphoton invariant mass distribution in events categorized using two independent boosted decision trees. Twelve categories are used for events where top quarks decay hadronically and eight for events where at least one top quark decays leptonically. Assuming a CP-even coupling, the process is observed with a significance of 5.2 standard deviations. The production process is not observed and an upper limit on its rate of 12 times the Standard Model expectation is set. A CP-mixing angle greater than is excluded at 95% confidence level.